Strings

Pages: 12
How does one break down a string into individuals chars? and then convert them to ints (alphanumeric a=1 b=2 c=3 etc)
Last edited on
I guess you could convert the string into a C-string: http://www.cplusplus.com/reference/string/string/c_str/

and then cast each individual char to an int.
std::string has the [] operator and the method at which allow to get a specific character in it.
by using strlen & strcpy in #include <string>

1
2
3
4
5
6
7
8
9

string str = "ABCDEFG";
char *buf = new char[strlen(str.c_str())];
strcpy(buf, str.c_str());

for(int i=0; i<7; i++)
{
cout<<buf[i]<<endl;
}

this converts your string to char
Last edited on
As i missed, but Bazzy picked up on, this can be simplified a lot to:

1
2
3
4
std::string str = "ABCDEFG";

for(int i=0;str[i];i++)
  cout << (int)str[i] << "  ";
Last edited on
Remember that std::strings can have null characters and that loop would terminate at the first occurrence of one of those
Could you elaborate please on what you mean by a null character mid-string?
Last edited on
@mcleano

a space " "
But this code compiles fine:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>

int main()
{
    std::string str = "abcde fghij";
    
    for(int i=0;str[i];i++)
     std::cout << str[i] << " ";
    
    std::cin.get();
    return 0;
}


and gives the correct output: a b c d e f g h i j
The null character '\0' is used as terminator for character arrays when used as strings but not for std::strings
Try to see what the output of this would be:
1
2
3
4
std::string str = "abcde fghij";
str[5] = 0;
for(int i=0;str[i];i++)
   std::cout << str[i] << " ";
hes is also trying to convert to alphanumeric
Btw, you can't simply cast a char to an int, you will get the ASCII value. You would need to do some arithmetic on chars to get the "correct" value:
str[i] - ('a' - 1); //this gives 'a' == 1, 'b' == 2, etc

Use Hammurabi's code instead.
Last edited on
This will convert letters to numbers, A (or a) to 1, B (or b) to 2, etc.

1
2
3
4
5
#include <cctype> // for toupper()

std::string str = "AbCdEfG";
for( int i = 0; i < str.size(); ++i )
    std::cout << (int)(std::toupper(str[i]) - 'A' + 1) << '\n';;
Oh yeah...forgot about captial letters >.>
By now, however, ASCII is so ubiquitous and EBCDIC so obsolete, that it can be pretty much assumed that code points [0;128) belong to ASCII.
The fact that UCS is backwards compatible with ASCII guarantees that in the future 'A'|32=='a' will always be true.

More or less, the same applies for arithmetics other than two's complement and bytes that are not octets.
Last edited on
Bazzy: That's a good point, but like helios said, screw EBCDIC! What a strange encoding, with noncontiguous letters. 'i' + 1 != 'j' indeed!
Hammurabi, your code works well but i can't get it to ignore spaces... And for the string problem i solved it by taking the easy way out by using a char array
Seems simple enough. And I don't see any problem with using string.

1
2
3
4
5
6
7
#include <cctype> // for toupper()

std::string str = "Ab Cd Ef G";
for( int i = 0; i < str.size(); ++i ){
    if( ! std::isspace( str[i] ))
        std::cout << (int)(std::toupper(str[i]) - 'A' + 1) << '\n';
}

Last edited on
It would be better
if ( std::isalpha ( str[i] ) )
Pages: 12