What is the order of characters when looping through - c++

Alright so this is super hard to put in words. So I have some code:
vector <char> Chars;
for(char c = '0'; c <='z'; c++) {
Chars.push_back(c);
}
And so it helps with adding characters to my character vector because it will add the select amount. Example:
vector <char> Chars;
for(char c = '0'; c <='9'; c++) {
Chars.push_back(c);
}
Gets you 0123456789 and:
vector <char> Chars;
for(char c = '0'; c <='Z'; c++) {
Chars.push_back(c);
}
Gets you 0123456789:;<=>?#ABCDEFGHIJKLMNOPQRSTUVWXYZ. How can I (a) get all of the characters and (b) what is the order of this? Why do they have some symbols after the numbers but not all of them?

(b) what is the order of this?
It is the order specified in the native character encoding of the system that you use. It is probably one of ASCII, ISO/IEC 8859 or UTF-8 all of which are identical in the range [0, 128).
Why do they have some symbols after the numbers but not all of them?
Because some of the symbols are before the numbers, and some more are after the letters.
That's just the order that was chosen by whatever committee designed the encoding. There's not necessarily a deep philosophy behind that choice. It may be an anomaly inherited from teletype codes that preceded computer systems.
How can I (a) get all of the characters
You can use numeric limits to find the minimum and maximum values, and a loop to iterate over them:
for(int i = std::numeric_limits<char>::min();
i < std::numeric_limits<char>::max(); i++) {
char c = i;
Chars.push_back(c);
}

I think the following will be of help to you:
What that means is, if you start your loop from '0' character, that corresponds to number 48 in that table. You are simply incrementing from there and going up until 'Z'. If you want just the alphanumeric characters, just seperate your loop to two pieces.

Each character has an ASCII code.
See https://en.wikipedia.org/wiki/ASCII
It just so happens that some symbols, but not all are located between '9' and 'A'.
As others have pointed out, it's probably not even guaranteed which symbols are where in that table. But I guess 'A' to 'Z', 'a' to 'z' and '0' to '9' are guaranteed to be contiguous.
Also, see:
Does C and C++ guarantee the ASCII of [a-f] and [A-F] characters?

Related

Garbage characters in C

Edited question
I understood my mistake in the code I had given in the original question, and the characters I was getting are garbage characters. Although, I still have a few questions about garbage characters in C:
Why can't the character be copied?
Do garbage characters have some pattern? Meaning that can you predict for an empty string what character can come, for an empty integer what will come, and so on.
When a variable is declared, why does it have a garbage character instead of being blank? Is there a specific reason of storing it with a garbage character?
For a string which is not null-terminated, will the same garbage character be printed on every OS? If yes, which one?
Are there the same garbage characters on every OS? Or are they different?
Is there a way to print these characters on the stdout buffer in C / C++?
If you see carefully in the character , there are some characters and numbers in it. Do they represent something?
Is there a list of garbage characters which can be printed in C / C++?
Original Question
Title of original question: Mysterious character output in C
I had come across this code in K & R:
int scanline (char str [], int lim) /* Line will be read in 'str []', while lim is the maximum characters to be read */
{
int c, len, j; /* 'len' will have the length of the read string */
j = 0; /* Initializing 'j' */
for (len = 0; (c = getchar ()) != EOF && c != '\n'; ++len) /* Reading a character one by one, till the user enters '\n', and checking for failure of 'getchar' */
{
if (len < (lim -2)) /* Checking that string entered has not gone beyond it's boundaries. '-2' for '\n' and '\0' */
{
str [j] = c; /* Copying read character into 'string [j]' */
++ j; /* Incrementing 'j' by 1 */
}
}
if (c == '\n') /* Checking if user has finished inputting the line */
{
str [j] = c; /* Copying newline into string */
++j;
++ len;
}
return len; /* Returning number of characters read */
}
In the K & R, it is known as getline, but I made changes, added comments, and thus defined it as scanline. To test this, I made a demo program:
#include <mocl/cancel.h>
int main (int argc, char **argv)
{
int len;
char str [50];
len = scanline (str, 50);
printf ("len = %d\n str = %s\n", len, str);
return 0;
}
The required headers and the function was in my own library, cancel.h. Then when I compiled my program, it was successful. Although, when I ran the executable, I got unexpected output (I cannot type it as I get a character which when I copy, it just gets pasted as 'm'):
The mysterious character is which when I copy, gets copied as the letter m. Also, when I run my program with different inputs, I get different mysterious outputs:
In another case, I get perfect output, just that a blank line is printed:
I had also come across this question, in which the user gets the same symbol.
What have I done till now?
I searched a lot, and I could not find any clue about this character, but if you see carefully, in the second image, I get more characters when I enter "hi this is ashish". One of them is the slash, and one is . But I get another character . I got this link which was showed how to reproduce it, and explained it, although I could not understand. When you run the code given there, you get a lot of characters, and one of them is . Although, even the author of that article could not copy it, and has not posted it. So here's the output:
That was the actual output, as that's not clear, here's a cut out version:
So basically I got to know that both the characters and are extended characters from a string. At that point, I actually figured out what was causing the problem in scanline.
The lines
if (c == '\n') /* Checking if user has finished inputting the line */
{
str [j] = c; /* Copying newline into string */
++j;
++ len;
}
were causing the problems as you were copying a newline into the string. It worked, but I'm not sure why, as doing that was just a guess. I searched but still could not find the reason.
My Questions
How does removing those lines make the program work properly?
What are the characters and ? What are they supposed to do and how did they appear over here?
Are there any more characters like this?
Why can't those characters be copied?
Is it Undefined Behavior?
There's some confusion here regarding the term garbage characters. What it refers to is any byte that resides in a variable that wasn't assigned in some well-defined way. The character A can be a garbage character if it happens to appear in (for example) a block of memory returned by malloc or an uninitialized char variable.
This is distinct from unprintable characters which are any character that does not have a well-defined representation when printed as characters. For example, ASCII codes 0 - 31 and 127 (0 - 1F and 7F hex) are control characters and therefore unprintable. There are also multibyte characters for which a particular terminal may not know how to render them.
To get into your specific questions:
Why can't the character (image) be copied?
As an unprintable character, its screen representation is not well defined. So attempting to copy and paste it from a terminal will yield unexpected results.
Do garbage characters have some pattern? Meaning that can you
predict for an empty string what character can come, for an empty
integer what will come, and so on.
The nature of garbage characters is that their contents are undefined. Trying to predict what uninitialized data will contain is a futile effort. The same piece of code compiled with two different compilers (or the same compiler with different optimization settings) can have completely different contents for any uninitialized data.
The standard doesn't say what values should go there, so implementations are free to handle it however they want. They could chose to leave whatever values happen to be at those memory addresses, they could choose to write 0 to all addresses, they could choose to write the values 0, 1, 2, 3, etc. in sequence. In other words, the contents are undefined.
When a variable is declared, why does it have a garbage character
instead of being blank? Is there a specific reason of storing it with
a garbage character?
Global variables and static local variables are initialized with all bytes zero, which is what the standard dictates. That is something that is done easily at compile time. Local variables on the other hand reside on the stack. So their values are whatever happens to be on the stack at the time the function is called.
Here's an interesting example:
void f1()
{
char str[10];
strcpy(str, "hello");
}
int main()
{
f1();
f1();
return 0;
}
Here is what a particular implementation might do:
The first time f1 is called, the local variable str is uninitialized. Then strcpy is called which copies in the string "hello". This takes up the first 6 bytes of the variable (5 for the string and 1 for the null terminator). The remaining 4 bytes are still garbage. When this functions returns, the memory that the variable str resided at is free to be used for some other purpose.
Now f1 gets called again immediately after the first call. Since no other function was called, the stack for this invocation of f1 happens to sit at the exact same place as the last invocation. So if you were to examine str at this time, you would find it contains h, e, l, l, o, and a null byte (i.e. the string "hello") for the first 6 bytes. But, this string is garbage. It wasn't specifically stored there. If some other function was called before calling f1 a second time, most likely those values would not be there.
Again, garbage means the contents are undefined. The compiler doesn't explicitly put "garbage" (or unprintable characters) in variables.
For a string which is not null-terminated, will the same garbage
character be printed on every OS? If yes, which one?
Here's one of those places you're confusing garbage and unprintable. In your specific case, the garbage character happens to be unprintable, but it doesn't have to be. Here's another example:
void f3()
{
char str1[5], str2[5];
strcpy(str1, "hello");
strcpy(str2, "test");
printf("str1=%s\n", str1);
}
Let's suppose the compiler decides to place str2 immediately after str1 in memory (although it doesn't have to). The first call to strcpy will write the string "hello" into str1, but this variable doesn't have enough room the the null terminating byte. So it gets written to the next byte in memory, which happens to be the first byte of str2. Then when the next call to strcpy runs, it puts the string "test" in str2 but in doing so it overwrites the null terminating byte put there when str1 was written to.
Then when printf gets called, you'll get this as output:
str1=hellotest
When printing str1, printf looks for the null terminator, but there isn't one inside of str1. So it keeps reading until it does. In this case there happens to be another string right after it, so it prints that as well until it finds the null terminator that was properly stored in that string.
But again, this behavior is undefined. A seemingly minor change in this function could result in str2 appearing in memory first. The compiler is free to do as it wishes in the regard, so there's no way to predict what will happen.
Are there the same garbage characters on every OS? Or are they
different?
I believe you're actually referring to unprintable characters in this case. This really depends on the character set of the OS and/or terminal in question. For example, Chinese characters are represented with multiple bytes. If your terminal can't print Chinese characters, you'll see some type of code similar to what you saw for each of the bytes. But if it can, it will display it in a well-defined manner.
Is there a way to print these characters on the stdout buffer in C /
C++?
Not as characters. You can however print out their numerical representations. For example:
void f4()
{
char c;
printf("c=%02hhX\n", (unsigned char)c);
}
The contents of c are undefined, but the above will print whatever value happens to be there in hexadecimal format.
If you see carefully in the character (image),
there are some characters and numbers in it. Do they represent
something?
Some terminals will display unprintable characters by printing a box containing the Unicode codepoint of the character so the reader can know what it is.
Unicode is a standard for text where each character is assigned a numerical code point. Besides the typical set of characters in the ASCII range, Unicode also defines other characters, such as accented letters, other alphabets like Greek, Hebrew, Cyrillic, Chinese, and Japanese, as well as various symbols. Because there are thousands of characters defined by Unicode, multiple bytes are needed to represent them. The most common encoding for Unicode is UTF-8, which allows regular ASCII characters to be encoded with one byte, and other characters to be encoded with two or more bytes as needed.
In this case, the codepoint in question is 007F. This is the DELETE control character, which is typically generated when the Delete key is pressed. Since this is a control character, your terminal is displaying it as a box with the Unicode point for the character instead of attempting to "print" it.
Is there a list of garbage characters which can be printed in C /
C++?
Again, assuming you really mean unprintable characters here, that has more to do with the terminal that's displaying the characters that with the language. Generally, control characters are unprintable, while certain multibyte characters may or may not display properly depending on the font / character set of the terminal.
For starters the function returns incorrect value of len. Let's assume that lim is equal to 2.
In this case in the loop there will be written nothing in the array due to the condition
if (len < (lim -2))
However after the first iteration of the loop len will be increased.
for (len = 0; (c = getchar ()) != EOF && c != '\n'; ++len)
^^^^^
In the second iteration again there will be written nothing in the array diue to the same condition
if (len < (lim -2))
but len will be increased.
for (len = 0; (c = getchar ()) != EOF && c != '\n'; ++len)
^^^^^
Thus nothing will be written in the array but len will be increased until for example the new line character will be encountered.
So the function is invalid. Moreover it is supposed that the function will append the read string with the terminating zero. But this is not done in the function. So you may not output the character array as a string.
The function can be written the following way
int scanline( char str [], int lim )
{
int len = 0;
int c;
while ( len < lim - 1 && ( c = getchar () ) != EOF && c != '\n' )
{
str[len++] = c;
}
if ( len < lim - 1 && c == '\n' ) str[len++] = c;
if ( len < lim ) str[len++] = '\0';
return len;
}

Sorting upper and lower case words in vector of strings

I'm trying to alphabetize words in a vector of strings and my program is distinguishing between upper and lowercase letters, so uppercase words always appear first in the sorted list. I can think of potentially really cumbersome ways to make sure the upper case words go in their place, but is there a simple way to do this?
Here is my code:
for (int i = 0; i < str.size(); i++)
{
for (int j = 0; j < str.size(); j++)
{
if (str.at(i) > str.at(j))
{
temp = str.at(j);
str.at(j) = str.at(i);
str.at(i) = temp;
}
}
}
Also, this is for a programming assignment, so I am not allowed to use built in C++ functions to do this and I have to use a vector.
The trick to solving this is to write a replacement for this line
if (str.at(i) > str.at(j))
that performs case-insensitive comparison. Start by writing a signature for it:
bool greaterThanIgnoreCase(const string& left, const string& right) {
...
}
Now you can replace your if condition with a call to this new function:
if (greaterThanIgnoreCase(str.at(i), str.at(j)))
Finally, you need to provide an implementation of greaterThanIgnoreCase function. This is the core of the problem, so you would need to do it yourself. The trick to it is using toupper or tolower function on each character of strings left and right, and compare them one character at a time. If you run out of characters in one of the strings, the one with some characters remaining should be considered greater.
The only thing you need to do is to compare lowercased characters. If you are not allowed to use any built-in functions, you can do it manually.
Since lowercase characters are located in ASCII table starting from 97, and uppercase - starting from 65, you can simply add 32 to the uppercase char to get its lowercase equivalent.
char lowerCase(char c)
{
if (c >= 'A' && c <= 'Z') // if char is uppercase
return (char)(c + 32); // return its lowercase equivalent
else
return c;
}
Then, you can do the following in your if condition:
if (lowerCase(str.at(i)) > lowerCase(str.at(j)))
Note that you shouldn use lowerCase only when you compare, but not when you assign.
Here is the working IDEOne demo.
If you have a vector of strings then you could/should use the STL library, which offers sort or stable_sort for a given comparison function (that can be a simple lambda expression, functor, function,...).
Update: You're not allowed to use built-in types except vector.
In this case you can develop easily a replicate of what STL does.
Define a sort function that receives a comparator (for a vector of strings).

remove non alphabet characters from string c++ [duplicate]

This question already has answers here:
How to strip all non alphanumeric characters from a string in c++?
(12 answers)
Closed 6 years ago.
I'm trying to remove all non alphabet characters from an inputed string in c++ and don't know how to. I know it probably involves ascii numbers because that's what we're learning about. I can't figure out how to remove them. We only learned up to loops and haven't started arrays yet. Not sure what to do.
If the string is Hello 1234 World&*
It would print HelloWorld
If you use std::string and STL, you can:
string s("Hello 1234 World&*");
s.erase(remove_if(s.begin(), s.end(), [](char c) { return !isalpha(c); } ), s.end());
http://ideone.com/OIsJmb
Note: If you want to be able to handle strings holding text in just about any language except English, or where programs use a locale other than the default, you can use isalpha(std::locale).
PS: If you use a c-style string such as char *, you can convert it to std::string by its constructor, and convert back by its member function c_str().
If you're working with C-style strings (e.g. char* str = "foobar") then you can't "remove" characters from a string trivially (as a string is just a sequence of characters stored sequentially in memory - removing a character means copying bytes forward to fill the empty space used by the deleted character.
You'd have to allocate space for a new string and copy characters into it as-needed. The problem is, you have to allocate memory before you fill it, so you'd over-allocate memory unless you do an initial pass to get a count of the number of characters remaining in the string.
Like so:
void BlatentlyObviousHomeworkExercise() {
char* str = "someString";
size_t strLength = ... // how `strLength` is set depends on how `str` gets its value, if it's a literal then using the `sizeof` operator is fine, otherwise use `strlen` (assuming it's a null-terminated string).
size_t finalLength = 0;
for(size_t i = 0; i < strLength; i++ ) {
char c = str[i]; // get the ith element of the `str` array.
if( IsAlphabetical(c) ) finalLength++;
}
char* filteredString = new char[ finalLength + 1 ]; // note I use `new[]` instead of `malloc` as this is C++, not C. Use the right idioms :) The +1 is for the null-terminator.
size_t filteredStringI = 0;
for(size_t i = 0; i < strLength; i++ ) {
char c = str[i];
if( IsAlphabetical(c) ) filteredString[ filteredStringI++ ] = c;
}
filteredString[ filteredStringI ] = '\0'; // set the null terminator
}
bool IsAlphabet(char c) { // `IsAlphabet` rather than `IsNonAlphabet` to avoid negatives in function names/behaviors for simplicity
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
}
I do not want to spoil the solution so I will not type out the code, only describe the solution. For your problem think of iterating through your string. Start with that. Then you need to decide if the currently selected character is part of the alphabet or not. You can do this numerous different ways. Checking ASCII values? Comparing against a string of the alphabet? Once you decide if it is a letter, then you need to rebuild the new string with that letter plus the valid letters before and after that you found or will find. Finally you need to display your new string.
If you look at an ascii table, you can see that A-Z is between 65-90 and a-z is between 97-122.
So, assuming that you only need to remove those characters (not accentuated), and not other characters from other languages for example, not represented in ascii, all you would need to do is loop the string, verify if each char is in these values and remove it.

Count occurrences of each letter in a file?

How to find the occurrence of letters A-Z regardless(ignore case) in a optimized way even if the file size is as large as 4GB or more ? What could be the different implementations possible in C++/C ?
One implementation is :
Pseudocode
A[26]={0}
loop through each character ch in file
If isalpha(ch)
A[tolower(ch)-'A']+ = 1
End If
end loop
Not much optimization left, I think.
Instead of computing tolower()-'A' for each element, just count occurrences of each character (in a char[256] accumulator), and do the case-aware computation afterwards (Might be more efficient or not, just try).
Be sure to use buffered input (fopen, perhaps assign larger buffer with setvbuf).
Eg:
acum[256]={0}
loop through each character 'c' in file
acum[c]++
end loop
group counts corresponding to same lowercase/uppercase letters
Also, bear in mind that this assumes ASCII or derived (one octet = one character) encoding.
This is not going to be instantaneous with 4GB. I see know way to do what you are doing much faster.
In addition, your code wouldn't handle tabs, spaces or other characters. You need to use isalpha() and only increment the count if it returns true.
Note that isalpha() is extremely fast. But, again, this code would not be instantaneous with a very large input.
TCHAR a[26] = { 0 };
for (int i = 0; i < length; i++)
{
if (isalpha(text[i]))
{
a[tolower(text[i]) - 'a']++;
}
}

C++ place char in corresponding slot in array

I need to store a letter a-z char to its corresponding slot in a size 0 - 25 array in c++. What's the best way to do this without a lot of if statements?
You can determine character index the following way:
int index = yourCharacter-'a';
And then use that index to store what you need
To work out the index just subtract 'a' from a char variable that holds the characters a-z. For example:
char c='x';
int index=(int)(c-'a');
Is this what are you looking for?
char c;
...
arrayName[c - 'a'] = value;
Edit
Not homework, so I'll clarify my answer.
const char * letters = "abcdefghijklmnopqrstuvwxyz";
Stores the letters from 'a' to 'z' in in an array. (Note that the array size is 27, not 26, because of the null character at the end of the string.)
Characters are just integers, so you can do arithmetic on them.
char c = ...;
array[c - 'a'] = c.
Note that upper case characters are distinct from lower case ones, so you'll need to handle them separately (if required).
if (c >= 'A' && c <= 'Z')
c += 'a' - 'A'; // make lower case
If you want to be portable, you can't use the solution proposed by most
others: 'a' to 'z' are not necessarily contiguous. The surest
solution is to look the letter up in a table, and use the index of that
table, e.g.:
char letters[] = "abcdefghijklmnopqrstuvwxyz";
int index = std::find( begin( letters ), end( letters ) - 1, ch )
- begin( letters );
if ( index < size( letters ) - 1 )
// it's good
else
// character wasn't a (lower case) letter.
Note the - 1 for end and size: this is because letters has an
additional '\0' at the end.
Note that in a lot of cases, on a modern machine, it may be just simpler
to use an array of 256 entries; the difference in space isn't likely to
cause you to run out of memory, and the code to manage it will be a lot
simpler.
char letters[26];
for (char c = 'a'; c <= 'z'; c++)
{
letters[c - 'a'] = c;
}