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

int main()
{
  int count,i,j;
  char str[50],sub_str[50];
  printf("Enter the main string : ");
  gets(str);
  printf("Enter the sub string : ");
  gets(sub_str);
  printf("%s %s\n",str,sub_str);
  int l_str, l_sub_str;
  l_str=strlen(str);
  l_sub_str=strlen(sub_str);
  for(i =0 ;i<(l_str); i++)
  {
      if(str[i]==sub_str[0])
      {
          count=1;
          for( j = 0; j<l_sub_str;j++)
            if(str[i+j]==sub_str[j])
                {
                    count++;
                    printf("%d \n",i);
                }
            else
                break;
       }
       if(count==l_sub_str)
          break;
  }
  if(count==l_sub_str)
    printf("String found at position %d  \n",i+1);
  else
    printf("No sub string \n");
  for(i=0;i<l_str;i++)
    printf("%c",str[i]);
  printf("\n");
}

Example data:

MAIN STRING: qwerty is
SUB STRING: is
POSITION:1

This the code to check whether a sub-string is present in a string or not and tell the position if it's present. In the above code, it has output at the end. For that output it should show, POSITION as 8 but it is showing 1. WHY?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

1 Answers1

0

The problem is not directly related to your use of gets(), though (as noted in comments) you should never use gets() because it is too dangerous.

The trouble is that:

  1. You don't initialize count so if the first letter of the substring is not present in the string, it is never initialized, and
  2. You start the inner loop with j = 0, which means you check the first letter twice, and count it twice, so when the search finishes with a match, you've set count too big. A simple fix is to start the loop with j = 1; an alternative fix would set count = 0; before the loop.

Replacing gets() with fgets() plus strcspn() and checking for EOF yields code like this:

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

int main(void)
{
    char str[50], sub_str[50];

    printf("Enter the main string : ");
    if (fgets(str, sizeof(str), stdin) == 0)
    {
        fprintf(stderr, "EOF\n");
        exit(EXIT_FAILURE);
    }
    str[strcspn(str, "\n")] = '\0';

    printf("Enter the sub string : ");
    if (fgets(sub_str, sizeof(sub_str), stdin) == 0)
    {
        fprintf(stderr, "EOF\n");
        exit(EXIT_FAILURE);
    }
    sub_str[strcspn(sub_str, "\n")] = '\0';

    printf("Haystack = [%s]\nNeedle = [%s]\n", str, sub_str);

    int l_str = strlen(str);
    int l_sub_str = strlen(sub_str);
    int count = 0;
    int i;
    for (i = 0; i < l_str; i++)
    {
        if (str[i] == sub_str[0])
        {
            count = 1;
            for (int j = 1; j < l_sub_str; j++)
            {
                if (str[i + j] == sub_str[j])
                    count++;
                else
                    break;
            }
        }
        if (count == l_sub_str)
            break;
    }

    if (count == l_sub_str)
        printf("String found at position %d (%s)\n", i + 1, &str[i]);
    else
        printf("No sub string\n");
    return 0;
}

Sample output (program name ss11):

$ ./ss11
Enter the main string : qwerty is
Enter the sub string : is
Haystack = [qwerty is]
Needle = [is]
String found at position 8 (is)
$ ./ss11
Enter the main string : qwerty is
Enter the sub string : z
Haystack = [qwerty is]
Needle = [z]
No sub string
$ ./ss11
Enter the main string : qwerty is
Enter the sub string : y
Haystack = [qwerty is]
Needle = [y]
String found at position 6
$ ./ss11
Enter the main string : qwerty isosceles isomer isotropic isotopes
Enter the sub string : iso
Haystack = [qwerty isosceles isomer isotropic isotopes]
Needle = [iso]
String found at position 8 (isosceles isomer isotropic isotopes)
$ ./ss11
Enter the main string : qwerty isosceles isomer isotropic isotopes
Enter the sub string : isot
Haystack = [qwerty isosceles isomer isotropic isotopes]
Needle = [isot]
String found at position 25 (isotropic isotopes)
$ ./ss11
Enter the main string : qwerty isosceles isomer isotropic isotopes
Enter the sub string : isoto
Haystack = [qwerty isosceles isomer isotropic isotopes]
Needle = [isoto]
String found at position 35 (isotopes)
$
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278