How to search a string for a word/character and return its position?

This is a discussion on How to search a string for a word/character and return its position? within the C Programming forums, part of the General Programming Boards category; If I had a string, say: "This is a string" How would I search for a particular word or character ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    20

    How to search a string for a word/character and return its position?

    If I had a string, say: "This is a string"

    How would I search for a particular word or character and get it's position into an integer?

    For instance, say I wanted to get the position of "a", which would be 3. Or if I wanted the position of "is" which is 2.

    I know how to return the position of a character in a string like this: "a b c d e f g" using a for loop, and within the for loop, counting the number of spaces. But that is using a simple compare of the current character with the desired character.

    I am confused how to do this when you have a mix of characters and words.

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    20
    strstr returns a pointer to the position of the word in the string. After I have this pointer, do I then use that pointer in a for loop and count the number of spaces from the pointer until the end of the string, then do some math:

    (total number of spaces) - (number of spaces after word) = (number of spaces before word)

    Is that the idea?

    Also, if I am searching a string of single characters and words ie: "a b c foo d e f foo", I need to tell it what string to search for. Can I specify a string with length of one? ie:

    char str[] = "b";

  4. #4
    Registered User Armen Naxxe's Avatar
    Join Date
    May 2009
    Posts
    1
    Maybe this can help.
    Code:
    char *str = "This is a string";
    char *tmp = strstr(str, "string");
    int space = 0;
    for (int i=0; i<str - tmp; i++) if (str[i] == ' ') space++;

  5. #5
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    Quote Originally Posted by NewbGuy
    strstr returns a pointer to the position of the word in the string
    you can subtract pointers in one array
    Attached Files Attached Files
    Last edited by c.user; 05-08-2009 at 07:38 PM. Reason: confused words

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21