Thread: How to read single characters in a string

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    6

    Arrow How to read single characters in a string

    I'm just starting out in C++ and the only thing I have had is a High School class about it. I'm making a proram where I need to read each character in a string individually. Is there a way to read character #3 or #4 or whatever in a string and assign that to a variable?
    My compiler is Borland C++ BuilderX.

    Code Example of what I have so far:

    Code:
    #include <iostream.h>
    #include <conio.h>
    //and include a few other files....
    
    main()
    {
    char Words[20], Character;
    int CharNum;
    
    cout << "Enter a few words: ";
    cin   >> Words;
    
    /*
    Now at the end of the string the variable would equal NULL right?
    */
    
    do
    {
    Character = /*Whatever command that sets Character to character number CharNum in the string "Words"*/
    
    //Run a switch statement on that character
    
    ++CharNum;
    
    }
    while (Character != NULL);
    
    return 0;
    }
    Please excuse any syntax errors as I typed this just now.

    Thanks for reading this
    Last edited by MaxxMan-X; 01-20-2005 at 08:51 PM.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Well you'll wanna work on indentation - makes it a lot easier to read (code tags preserve spacing - it would appear that you added the tags in an edit - soe just make sure you put the spacing back)

    Now at the end of the string the variable would equal NULL right?
    Correct

    Is there a way to read character #3 or #4 or whatever in a string and assign that to a variable?
    Character[2] and Character[3], respectively. The first element in the array can be referenced as array[0], so all the element numbers are shifted down by one. In your case, Character[19] would be a null character ( '\0' ).

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Now at the end of the string the variable would equal NULL right?
    Eh... No. At the end of the string would be the null character (which, as sean pointed out, is '\0'). Only a pointer can take on NULL as a value. If you set your character equal to something beyond the end of the string, you will simply read whatever is in that memory slot (which could be garbage, or could be something your program is using for something else, etc).

    Essentially, syntax such as:
    char array[20] = // some string here...
    char c = array[n]; // n is an integer
    Looks at the address of array (equivalently, the address of array[0], the first element of the array), and then incrememts n times (moving to an address of: address_of(array[0]) + n*sizeof(char) -- pseudo-code used for clarity).

    So, instead of 'Character != NULL', you really want 'Character != '\0''.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    6
    Alll Right! It's working!
    Thanks guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 06-16-2011, 11:59 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  4. Replies: 1
    Last Post: 05-30-2003, 02:31 AM