Thread: very basic question about char *

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    12

    very basic question about char *

    I'm new to c and need to take in a string (char *) and cut it down into its individual words. I'm not sure what the most efficient way to do this would be. For example if given the string "This is a string", I need to turn it into the four strings "This", "is", "a", and "string". There will only be a maximum of 4 words in the input string, so I'm trying to figure out what the best strategy would be. Thanks for any help.

    I'm also kind of confused about how a char * can point to a string. Why isn't it just a pointer to one character? Thanks.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    The process of dividing string to its works is called tokenisation. Have a look the function strtok on how tokenise the string given the de-eliminator character. For example:

    Code:
    char str[] = "This is a test";
    chatr *p = strtok( str, ' ');
    printf(p);
    But its more than that if you had to tokenise the whole string. Have a google or search the forum. You will find dozens of post related this topic.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    Quote Originally Posted by ssharish2005 View Post
    The process of dividing string to its works is called tokenisation. Have a look the function strtok on how tokenise the string given the de-eliminator character. For example:

    Code:
    char str[] = "This is a test";
    chatr *p = strtok( str, ' ');
    printf(p);
    But its more than that if you had to tokenise the whole string. Have a google or search the forum. You will find dozens of post related this topic.

    ssharish
    Ok, cool thanks. Just as an aside, is there any difference between saying
    Code:
    char str[] = "String";
    and
    Code:
    char * str = "String";
    ?

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    Quote Originally Posted by Bayint Naung View Post
    thank you very much!

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    Ok, cool thanks. Just as an aside, is there any difference between saying
    
    Code:
    char str[] = "String";
    and 
    
    Code:
    char * str = "String";
    ?
    Yes there is a different, the former is a string which can be changes. So basically you're initialising the char array str[] to literal string. But with the later you're literal string is stored in readonly memory and pointer *str is pointing to that memory location. This basically means that you cannt later the string. But with the former method you can alter the string if needed later in the code.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. question about functions and char *
    By Bittrexx in forum C Programming
    Replies: 4
    Last Post: 07-22-2003, 12:27 PM
  5. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 10:14 PM