Thread: Question about strings n chars

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    3

    Question about strings n chars

    I have just revived my interest in c and I have the following problem:

    I am trying to store an array filled with strings:
    Code:
     arraystrings[]={" 5 ms", " 10ms", "15 ms"};
    Then I need to select one of the array string elements :
    Code:
     array[0]=" 5 ms"
    Now I need to extract the individual characters out of the selected string:
    Code:
    char array[0][0]=' '
    char array[0][1]='5'
    char array[0][2]=' '
    char array[0][3]='m'
    char array[0][4]='s'
    Can someone explain to me how to do this? How do you initalize the array and then select a string element the select a character. An example would be great.

    I am trying to save a large string array and then output the characters for the chosen string element on an LCD display one by one but I haven't crossed this bridge yet


    Thanks
    Chris

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Um... Something like this:

    Code:
    char *numwords[] = {"Zero", "One", "Two", "Three", "Four", "Five" };
    numwords[0] would be "Zero", and numwords[0][0] would be 'Z'.

    Edit: FYI, as I have declared it, you are not allowed to edit these C-strings, btw. (ie. You can't overwrite them with new data.)

  3. #3
    Registered User
    Join Date
    Jun 2008
    Location
    Houston, Texas
    Posts
    43
    I just finished doing something like this.

    I used str.length, substr and a loop. It worked pretty effectively, just set the substring length to one character, and limit the loop with the returned value from the length.


    Edit: Oh yeah, I didn't use c-strings.


    um, play around with the string class, it's good fun, and it would make this pretty easy.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If this is C++, then std::string is suggested instead. And if you want an array of strings, then I suggest you take a look into boost::array for better C++-style arrays.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    3

    Thats it

    Yep I knew I was pretty close.
    You nailed it

    thanks
    chris

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    don't use substr just for getting a single character. use the at() function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concatenating C style Strings Question
    By bengreenwood in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2009, 03:19 PM
  2. Question about connecting strings and nul character
    By Degenko in forum C Programming
    Replies: 2
    Last Post: 06-03-2007, 10:15 AM
  3. simple question about strings
    By panfilero in forum C Programming
    Replies: 2
    Last Post: 11-03-2005, 01:57 AM
  4. question about char's in .net
    By ssjnamek in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2005, 03:45 PM
  5. Strings question
    By kimimaro in forum C Programming
    Replies: 10
    Last Post: 03-15-2005, 12:14 AM