Thread: strings

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    79

    strings

    Hello,

    I have some questions about strings...

    If I use scanf to scan in a string, then I want to take parts of the string to do various processes, then how would I do it?

    eg. If the input that the user types in is "print this is cool"; then how could I get the "print" and "this is cool" into 2 different strings?

    Also, I know that this would be pretty similiar to what the above question is, but how could I take parts of a string for one of the string functions.

    eg. If I had a string with "hi my name is joe", then how could I take, say take the string from the first character to the 7th character? Similiarly for the 2nd character to the 8th character? And so on... I would like to use strcmp, ie. compare different parts of a string to parts of a string (or a string).

    Thanks
    Last edited by Mini; 05-24-2010 at 02:56 AM.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Since a string is just an array of characters ending with \0. You can chop up a string that is a sentence by replacing a space with \0. For example:

    "My name is Joe"

    to

    "My name\0is Joe"

    You can either do it manually or use a function like strtok. Obviously you need to assign the i character in the above example to a char pointer to be able to access it.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    79
    Hmm okay,

    But that wouldn't really help with the other part of the string... You see I need to make a program that searches arrays of strings...

    eg. If I type in "compare blah blah blah", then I need to find all strings in the array that contain "blah blah blah".

    So obviously I would want the "compare" and "blah blah blah" in different strings.

    So my idea is that I somehow get the length of "blah blah blah" and then use it to compare line by line each string of the main array that I need to search. But then again, how would I compare parts of a string?

    This is further complicated by the fact that I need to include other commands. eg. if I type "display" then it needs to show the string.

    I have no idea where to start
    Last edited by Mini; 05-24-2010 at 03:30 AM.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    There is a function called strstr() that looks for a sub string in a larger string. So:

    Code:
    char *str = strstr(string, "blah");
    Will return a pointer the the first occurrence of "blah" in string.

    BTW, when you say arrays of strings do you mean 2d arrays? If so you could iterate over the array and use strcmp to find matches.
    Last edited by Subsonics; 05-24-2010 at 03:43 AM.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    79
    Quote Originally Posted by Subsonics View Post
    BTW, when you say arrays of strings do you mean 2d arrays? If so you could iterate over the array and use strcmp to find matches.
    Yes I do. How would I do that?

    But my question now would be, how would I 'harvest' the user's input, say, "search blah blah blah"... then I would need to store the "search" into another array and the other bit into another array... how would I do this?

    Thanks

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You could use the same method of strcmp on a 2d array, like this, but replace the search term here with one that the user supplies.

    Code:
            char list[][10] = {"list", "of", "pointless", "strings", "but", "oh", "well"};
    
            int i;
            for(i = 0; i < 7; i++){
                    if(!strcmp(list[i], "oh"))
                            printf("Found a match of: %s\n", list[i]);
            }
    strcmp() returns zero if the strings are identical, hence the negation.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    79
    yeah, but the only problem with that is that my array may contain something like

    balh balh hi how are you
    this is hello
    world is cool

    (where each line represent the string)

    then if I search for "is" then it would return the 2nd and 3rd strings...

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    No, you could just use, the strstr function from post #4. At each index of your 2d array is a string with a "text much like this", then for each string in the 2d array, stop, call strstr and move on if nothing is found. If something is found, take appropriate action.

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    79
    Okay I figured out the command line stuff...

    How would I do this if I needed to use strcmp?

    I know that this would involve comparing different parts of the strings... but I'm not sure how I'd do that.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well strcmp just compares its arguments and returns a positive number if s2 comes before s1, negative number, or zero if they are equal. It doesn't really look for anything. If you had to use strcmp, you would basically end up writing strstr, or a better string-search algorithm.

    Code:
    for (i = 0, len = strlen (source); i < len; i++) {
       if (strncmp (source + i , search , strlen (search)) == 0) {
          return source + i;
       }
    }
    return NULL;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Problem with Strings, Please help!
    By varus in forum C++ Programming
    Replies: 8
    Last Post: 11-27-2006, 11:47 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM