Home > Software engineering >  Find index of word in string
Find index of word in string

Time:02-14

I want to write a function which will find index of word in string. For example if string is

This is word.

my function for string "word" should return number 3.

  • Note: functions from string.h library and auxiliary strings are not allowed.

How could I do this in C?

CodePudding user response:

I can't think of a solution better than this (though there might be better ones).

#include <stdio.h>

int main() {
    char word[] = "This is a word";
    int flag = 0, space = 0, pos = -1;
    for (int i = 0; word[i] != '\0'; i  ) {
        if (flag == 1) {
                break;
            }
        for (int j = 0; word[j] != '\0'; j  ) {
            if (flag == 1) {
                break;
            }
            else if (word[j 1] == '\0' || word[j 2] == '\0' || word[j 3] == '\0') {
                break;
            }
            else {
                if (word[j] == 'w' && word[j 1] == 'o' && word[j 2] == 'r' && word[j 3] == 'd') {
                    flag = 1;
                    pos = j;
                }
            }
        }
    }

    for (int i = 0; word[i] != '\0'; i  ) {
        if (word[i] == ' ' || word[i] == '!' || word[i] == '@') {// And many more symbols
            fchars  ;
        }
        else {
            break;
        }
    }

    if (flag == 1 && pos-1 > 0 && word[pos-1] == ' ') {
        for (int i = 0; i < pos; i  ) {
            if (word[i] == ' ') {
                space  ;
            }
        }
        printf("Found at position = %i\n", space 1-fchars);
    }
    else {
       printf("Not found!\n");
    }
    
}

CodePudding user response:

You can split the sentence by space to get the words and then match each word in the sentence with the word you want to match Please check this modified code:

#include<stdio.h>
 
int main()
{
    char word[] = "word";
    char string[100];
    gets(string);
    int curWordStart = -1;
    int curWordEnd = -1;
    int wordCount = 0;
    int i = 0;
    for (i = 0; string[i] != '\0'; i  )
    {
        if (string[i] == ' ')
        {
            int curWordLength = curWordEnd - curWordStart   1;
            if (curWordStart != -1 && curWordLength > 0)
            {
                wordCount  ;
                int foundMatch = 1;
                int j;
                int k = 0;
                for (j = curWordStart; j <= curWordEnd; j  ) {
                    if (word[k] == '\0') {
                        foundMatch = 0;
                        break;
                    }
                    if (word[k] != string[j])
                    {
                        foundMatch = 0;
                        break;
                    }
                    k  ;
                }
                if (word[k] != '\0')
                {
                    foundMatch = 0;
                }
                
                if (foundMatch == 1)
                {
                    printf("%d\n", wordCount);
                }
            }
            curWordStart = -1;
            curWordEnd = -1;
        }
        else if ((string[i] >= 'a' && string[i] <= 'z') || (string[i] >= 'A' && string[i] <= 'Z'))
        {
            if (curWordStart == -1) {
                curWordStart = i;
            }
            curWordEnd = i;
        }
    }
    int curWordLength = curWordEnd - curWordStart   1;
    if (curWordStart != -1 && curWordLength > 0)
    {
        wordCount  ;
        int foundMatch = 1;
        int j;
        int k = 0;
        for (j = curWordStart; j <= curWordEnd; j  ) {
            if (word[k] == '\0') {
                foundMatch = 0;
                break;
            }
            if (word[k] != string[j])
            {
                foundMatch = 0;
                break;
            }
            k  ;
        }
        if (word[k] != '\0')
        {
            foundMatch = 0;
        }
        
        if (foundMatch == 1)
        {
            printf("%d\n", wordCount);
        }
    }
    return 0;
}

It will print each position of the searched word in the sentence. If you want to just print the first one, you can easily modify it.

CodePudding user response:

Here are steps to follow:

  • you must specify precisely what is a word in the string.
  • measure the length len of the word to search
  • define an int index = 1
  • in a loop, using a pointer p starting at the beginning of the string:
    • advance p past all word delimiters (spaces, punctuation or non letters?)
    • if p is at end of string return 0 (not found).
    • measure the length len1 of the current word in the string
    • if len1 == len and all bytes are identical to those of the word, return index
    • otherwise skip the word by advancing p by len1, increment index and continue the loop.

Here is an implementation:

#include <stddef.h>

int isletter(char c) {
    /* assuming ASCII */
    return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
}

int word_index(const char *str, const char *word) {
    const char *p = str;
    size_t len, len1, i;
    int index = 1;
    for (len = 0; word[len]; len  )
        continue;
    for (;;) {
        while (!is_letter(*p))
            p  ;
        if (*p == '\0')
            return 0;
        for (len1 = 0; is_letter(p[len1]); len1  )
            continue;
        if (len1 == len) {
            for (i = 0; i < len && p[i] == word[i]; i  )
                continue;
            if (i == len)
                return index;
        }
        p  = len1;
        index  ;
    }
}
  • Related