Home > Enterprise >  How to check how many times a word appears in a string in all possible upper and lower case combinat
How to check how many times a word appears in a string in all possible upper and lower case combinat

Time:01-08

For example: "horse HORSE HORse HoRsE" the word horse appears 4 times. How can i do this in c? #include<stdio.h> #include<string.h> your textmain() your text{

char str[100];
int i,SL,count=0,flag=0;
printf("Give string:");
gets(str);
SL=strlen(str);
    for (i=0;i<SL;i  )
{
    if ((str[i]=='h')||(str[i]=='H'))
    {
        flag  ;
    }
    if ((str[i 1]=='o')||(str[i 1]=='O'))
    {
        flag  ;
    }
    if ((str[i 2]=='r')||(str[i 2]=='R'))
    {
        flag  ;
    }
    if ((str[i 3]=='s')||(str[i 3]=='S'))
    {
        flag  ;
    }
    if ((str[i 4]=='e')||(str[i 4]=='E'))
    {
        flag  ;
    }
    if (flag==5)
    {
        count  ;
        flag=0;
        i=i 5;
    }
}
printf("There are %d horse in the string",count);

} your text

it can count when i put horse in any possible combination like the one i gave as an example. But when the words have no space "horsehorse" it only counts the first word. And when i put horse in a sentence like "hello horse how are you today" it counts nothing. (sorry for my english)

CodePudding user response:

You need to make a copy to make sure that haystack is modifiable or not modify it at all. Also, use functions. strnicmp is not universal as does not check if strings have different sizes, but was not needed in this case. You can also add some parameter checks.

char strnicmp(const char *haystack, const char *needle, size_t len)
{
    while(len--)
        if(tolower((unsigned char)*haystack  ) != tolower((unsigned char)*needle  ))
            return 1;
    return 0;
}

size_t count(const char *haystack, const char *needle, int overlap)
{
    size_t result = 0;
    size_t hsize = strlen(haystack);
    size_t nsize = strlen(needle);

    for(size_t pos = 0; pos   nsize <= hsize;)
    {
        if(!strnicmp(haystack   pos, needle, nsize)) 
        {
            result  ;
            pos  = overlap ? 1 : nsize;
        } else pos  ;
    }
    return result;
}

int main(void)
{
    printf("%zu\n", count("horSeHORse", "hORsE",0));
    printf("%zu\n", count("horSe is", "hORsE",0));
    printf("%zu\n", count("dffd;dfsgd d;lgd;fslg ks;dfl kd;", "hORsE",0));
    printf("%zu\n", count("tatatatatata", "tata",0));
    printf("%zu\n", count("tatatatatata", "tata",1));
}

https://godbolt.org/z/YzaMrKGfz

Thank you for your answer. But is there a way to do it with out the use of tolower and strncmp

It is a very good practice to use functions but if you do not want to use standard ones you can always write your own ones;

int mytolower(const int x)
{
    if(x >= 'A' && x <= 'Z') return x - ('A' - 'a');
    return x;
}

char strnicmp(const char *haystack, const char *needle, size_t len)
{
    while(len--)
        if(mytolower(*haystack  ) != mytolower(*needle  ))
            return 1;
    return 0;
}

CodePudding user response:

Set flag to zero before each check, not only after a successful match.

Do not add anything to i when finding a match. Adding 5 resulted in moving beyond the second “h” in “horsehorse” because the loop adds 1 anyway. There is no need to add anything because the loop adds 1 to i in each iteration, and adding more than 1 is an error because, for some strings, a new match can start before the end of the current match. For example, “yoyoyo” contains two matches for “yoyo”.

CodePudding user response:

You can create a for loop that checks through the word and coverts it into lowercase using tolower. After that you could use strncmp that compares the words to horse

#include<stdio.h>
#include<string.h>
#include<ctype.h>

int main() {
    char str[100];
    int i,SL,count=0;
    printf("Give string:");
    fgets(str, sizeof(str), stdin);
    str[strcspn(str, "\n")] = '\0';
    SL=strlen(str);

    for (i = 0; i < SL; i  ) {
        str[i] = tolower(str[i]);
    }

    for (i=0; i<SL; i  ) {
        if (strncmp(str   i, "horse", 5) == 0) {
        count  ;
            i  = 4;
        }
    }
    printf("There are %d horse in the string", count);
    return 0;
}

Example output 1:

Give string:horsehorse
There are 2 horse in the string

Example output 2:

Give string:hello i am a hORsE
There are 1 horse in the string

CodePudding user response:

A couple issues here: first off, please don't post incomplete code that doesn't compile. Also, you're using some unsafe, deprecated functions here that are an accident just waiting to happen: You should change gets to fgets.

For the logic of the program, set flag to 0 outside the if statement and skip manually incrementing i altogether

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFSIZE 512

int main() {
  char str[BUFFSIZE];
  int i, SL, count = 0, flag = 0;
  printf("Give string:");
  fgets(str, BUFFSIZE, stdin);
  SL = strlen(str);

  for (i = 0; i < SL; i  ) {
    if ((str[i] == 'h') || (str[i] == 'H')) {
      flag  ;
    }
    if ((str[i   1] == 'o') || (str[i   1] == 'O')) {
      flag  ;
    }
    if ((str[i   2] == 'r') || (str[i   2] == 'R')) {
      flag  ;
    }
    if ((str[i   3] == 's') || (str[i   3] == 'S')) {
      flag  ;
    }
    if ((str[i   4] == 'e') || (str[i   4] == 'E')) {
      flag  ;
    }
    if (flag == 5) {
      count  ;
    }
    flag = 0;
  }
  printf("There are %d horse in the string", count);
}
  •  Tags:  
  • c
  • Related