find a string based on the location of another string

This is a discussion on find a string based on the location of another string within the C Programming forums, part of the General Programming Boards category; I am looking for a hint (I am not a C programmer but have a need to maintain a C ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    2

    find a string based on the location of another string

    I am looking for a hint (I am not a C programmer but have a need to maintain a C program) this function may already exist but I cannot find it.

    I have 2 char string variables that contain x number of words in each. The number of words is always equal. if str1 has 4 words then str2 will have 4 words

    char str1[] = "word1 word2 word3 word4";
    char str2[] = "reply1 reply2 reply3 reply4";

    I need to search str1 matching on one of the words and if I have a match I need to return the value in str2 for the same number word.

    for example:

    search str1 for the word "word3" (a match would be found in the third word in the string)
    if match found return "reply3" (return the value of the the third word in the string)


    In REXX I would do it as:

    wordnum = wordpos('word3',str1)
    return = WORD(str2,wordnum)

    return would contain reply3 after these 2 statements were executed.

    thanks in advance for any help.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    to search for the substring you can use strstr

    to look for the end of word you can use strchr

    to copy the found word - strncpy
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    that's easy enough for searching in one array but it's harder with parallel arrays. you need to keep track of the number of words and then do the same thing on the parallel array for the same number of words.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define SEARCHWORD "word2"
    
    int main()
    {
        char str1[] = "word1 word2 word3 word4";
        char str2[] = "reply1 reply2 reply3 reply4";
        int i = 0, j = 0, k = 0;
        for (i = 0; 
            str1[i] && strstr(str1, SEARCHWORD) != &str1[i]; 
            i += strcspn(&str1[i], " ") + 1) j++;
        if (str1[i])
        {
            while (j-- > 0) k += strcspn(&str2[k], " ") + 1;
            printf("found starting at %s\n", &str2[k]);
        }
        return EXIT_SUCCESS;
    }

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    2
    Thanks for the example. It worked like a charm. The printf would display the str2 value I needed plus whatever was left . Like if I looked for word3 the printf would display reply3 reply4 so I added the strtok below and am now able to get exactly what I need. Thanks again.

    char *rp;

    rp = strtok(&str2[k], " ");

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find and replace within a string?
    By clancyPC in forum C Programming
    Replies: 3
    Last Post: 03-20-2009, 08:28 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 02:02 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM

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