You are here:

C++/Hello, on breaking down strings


I was wondering if there is a method in which i can break down a string into single characters.

as a matter of fact, i would like to make an algorithm to check if a string consists of all characters or not.

somebody told me to use atoi(&string) but this doesn't seem to work out too well.

Am I using the atoi function wrong, or is there a better method?

Thank you once again.


One thing I forgot to mention, if you wish to output a char as its numeric value rather than as a character then you have to convert it to another type of integer first, for example:

       int char_value( a_string[0] );
       std::cout << char_value;

Or more briefly:

       std::cout << int(a_string[0]);

Of course this may be negative if chars are negative by default for your compiler, in which case try casting the char object to an unsigned char first:

       std::cout << int( static_cast<unsigned char>(a_string[0]) );


I am afraid that your question makes no sense to me so I think what you actually asked was not what you intended. The only thing I can think of is that you have not realised exactly what characters are and the relationship between the character symbol and its numeric value. So I shall try to explain these areas. I apologise if I have not clarified what you wanted know - if not then please ask a follow up question which makes it plain to the likes of myself what you wish to do.

Characters strings are by definition composed of characters. Characters can include letters, digits, punctuation symbols, control codes etc. You can use array element syntax to access individual characters for both C++ style std::string and C style zero terminated arrays of char:

       a_string[0]     // access the first character of a string

A character is represented as an integer value. Which number maps to which character depends on the particular character set in use. For example most desktop machines use a character set in which the first 128 values (0 to 127 - or 000 0000 to 111 1111 in binary) match the characters defined by the ASCII character set. In this character set the character 0 is a null character, values 1 to 26 are control code values ctrl+A to ctrl+Z (as typed on a keyboard) and include some that keyboards usually have special keys for - ctrl+M is carriage return for example. Digits 0 to 9 start at 48 for 0 (which is 30 in hexadecimal) and uppercase letters at 65 (41 hexadecimal). Note that the values sent by a keyboard are generally totally different to the character values used in strings.

In C++ an object of type char is generally an 8-bit integer value - it may or may not be signed. In the simplest scheme each char type integer  in a character string represents one character. So when you define a string literal by placing the characters in double quotes:

       "A string literal"

You create a C-style zero terminated array of char. So the above string, using the ASCII character set encoding would be an array of char having the following values (in hexadecimal):

       41 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 00

The C library function atoi will convert a (C-style) string of digit characters to an integer value, so:

       int value = atoi("1234");

will take the C string of chars (in hexadecimal again) 31 32 33 34 00 and return an integer having the value 1234 decimal or 0000 0000 0000 0000 0000 0100 1101 0010 binary - assuming int objects are 32-bit integers for the compiler in question. To use the atoi function with a C-style string, just pass the name of the character array or a pointer to char - remember that the name of an array is equivalent to taking the address of the first character:

       char a_number_string[] = "1234";
       int value = atoi(a_number_string);

If you are using C++ std::string strings then you have to convert the C++ string to a C string first using the std::string c_str() member function:

       std::string a_number_string("1234");
       int value( atoi(a_number_string.c_str()) );  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2017 All rights reserved.