How to find an input sub-string in an input string in C

4 Answers

0 votes
#include <stdio.h> 
#include <string.h>

int main(void)
{   
     int i=0,j=0,found=0,count=0,sw=0;
     char str[50], sub[30];
     
     printf("Enter string: ");
     gets(str);
     printf("Enter sub-string: ");
     gets(sub);

     while (str[i]!=EOF)
     {
          if (str[i]==sub[j])
          {
               i++;
               j++;
               sw=1;
               if (j==strlen(sub))
               {
                    j=0;
                    found=1;
                    count++;
               }
          }
          else
          {
               if (sw==1)
               {
                    j=0;
                    sw=0;
               }
               else
                    i++;
          }
     }

     if (found==1)
        printf("Found %d times\n",count);
     else
     {
       if (found==0)
            printf("Not found");
     }
          
     return 0;
}
 

  
/*
run:
    
Enter string: programming
Enter sub-string: ram
Found 1 times
 
*/

 



answered Nov 14, 2016 by avibootz
edited Nov 14, 2016 by avibootz
0 votes
#include <stdio.h> 
#include <string.h>

int main(void)
{   
     int i=0,j=0,found=0,count=0,sw=0;
     char str[50], sub[30];
     
     printf("Enter string: ");
     gets(str);
     printf("Enter sub-string: ");
     gets(sub);

     while (str[i]!=EOF)
     {
          if (str[i]==sub[j])
          {
               i++;
               j++;
               sw=1;
               if (j==strlen(sub))
               {
                    j=0;
                    found=1;
                    count++;
               }
          }
          else
          {
               if (sw==1)
               {
                    j=0;
                    sw=0;
               }
               else
                    i++;
          }
     }

     if (found==1)
        printf("Found %d times\n",count);
     else
     {
       if (found==0)
            printf("Not found");
     }
          
     return 0;
}
 

  
/*
run:
    
Enter string: programming ram memory
Enter sub-string: ram
Found 2 times
 
*/

 



answered Nov 14, 2016 by avibootz
0 votes
#include <stdio.h> 
#include <string.h>

int main(void)
{   
     int count=0;
     char str[50], sub[30];
     
     printf("Enter string: ");
     gets(str);
     printf("Enter sub-string: ");
     gets(sub);

     char *p = str;
     
     while ( (p = strstr(p, sub)) != NULL) 
     {
        count++;
        p = p + strlen(sub);
     }
        
     if (count > 0)   
        printf("Found %d times\n", count);
     else
        printf("Not found");
          
     return 0;
}
 

  
/*
run:
    
Enter string: programming
Enter sub-string: ram
Found 1 times
 
*/

 



answered Nov 14, 2016 by avibootz
0 votes
#include <stdio.h> 
#include <string.h>

int main(void)
{   
     int count=0;
     char str[50], sub[30];
     
     printf("Enter string: ");
     gets(str);
     printf("Enter sub-string: ");
     gets(sub);

     char *p = str;
     
     while ( (p = strstr(p, sub)) != NULL) 
     {
        count++;
        p = p + strlen(sub);
     }
        
     if (count > 0)   
        printf("Found %d times\n", count);
     else
        printf("Not found");
          
     return 0;
}
 

  
/*
run:
    
Enter string: programming ram memory
Enter sub-string: ram
Found 2 times

*/

 



answered Nov 14, 2016 by avibootz
...