简体   繁体   中英

Why isn't my program printing out the correct message?

I'm trying to write a program to see if string 1 is a part of string 2. At the command prompt, I enter string 1, and then string 2. But the problem is, no matter what I type, my program keeps printing out the answer that "No, string 1 is not a part of string 2". I'm not sure what I could be doing wrong, is there something wrong with my for loop? Help is appreciated!

int string_part_of_other(void)
{
   char str1[20];
   char str2[20];
   int answer = 1;

   printf("Enter string 1:\n");
   scanf("%s", str1);

   printf("Enter string 2:\n");
   scanf("%s", str2);

   for (int i = 0; str1[i] != '\0'; i++)
   {
      for (int j = 0; str2[j] != '\0'; j++)
      {
         if (str1[i] != str2[j])
         {
            answer = 0;
         }
      }
   }

   return answer;
}

int main()
{
   int result;
   result = string_part_of_other();

   if (result == 1)
   {
      printf("Yes, string 1 is part of string 2.\n");
   }

   if (result == 0)
   {
      printf("No, string 1 is not part of string 2.\n");
   }

   return 0;
}

The problem is in your loop. You're comparing an entire string against one character.

for (int i = 0; str1[i] != '\0'; i++){
   for (int j = 0; str2[j] != '\0'; j++){ // here
      if (str1[i] != str2[j]) answer = 0; 
   }
}

strpbrk or strstr is a function that does this automatically, if that's what you would prefer.

You could also try this:

for (int i = 0; str1[i]; i++){
    int j = 0;
    for (; str2[j] && str1[i + j]; j++){
        if (str2[j] != str1[i + j]) break;
    }
    if (!str2[j]) return 1; /*
    * This means the loop broke because it reached the end of the
    * string, not because of a mismatch. Therefore, str2 is within str1
    */
}

return 0;

Also, I think it is your intention to say string 2 is part of string 1 , and not the other way around.

The algorithm you've written answers the following:

"For each character in string1, does it match every character in string2?"

If you step through the code in your head you should be able to figure out what's going wrong, and how to fix it.

Few remarks:

  • don't use scanf() . It doesn't do what you think it does, and it even goes unprotected without the ability to specify field width, so you should expect buffer overruns.

  • so use fgets(buf, sizeof(buf), stdin) instead (and beware of trailing newlines).

  • Don't reinvent the wheel: there's a function called strstr() in the C standard library which does exactly what you want, and it works correctly unlike your current ugly-hack-with-unreadable-nested-for-loops.

All in all:

int part_of()
{
    char buf1[LINE_MAX], buf2[LINE_MAX], *p;
    fgets(buf1, sizeof(buf1), stdin);
    fgets(buf2, sizeof(buf2), stdin);

    p = strchr(buf1, '\n');
    if (p) *p = 0;
    p = strchr(buf2, '\n');
    if (p) *p = 0;

    return strstr(buf1, buf2) != NULL;
}

Also, don't write if (func() == 1) {} then if (func() == 0) {} immediately afterwards - redundancy is bad. if (func()) {} else {} is fine.

For example, if your last char in str1 and last char in str2 do not match each other, the answer will be 0 . Even if str2 is a part of str1

you are just comparing all characters of str1 with that of str2 and even if one mismatch is there you set answer to zero .. wrong logic based on asked question I am assuming you want to check str1 to be a part of str2, the outer loop should be of the parent or containing string in this case str2 ...

    int answer=0;
    for(int i=0;str2[i]!='\0';i++) //traversing bigger string
    {
    if(str2[i]==str1[0])
    //if character of bigger string matches first  character small string
    {
    for(int j=0;str1[j]!='\0';j++)
    { 
    if(((i+j)<strlen(str2))&&(str1[j]!=str2[i+j])){
    break;}
    }//j
    if(str1[j-1]=='\0')
    {answer=1;
     break;}
    }//i
if (str1[i] != str2[j]) {
            answer = 0;
}

If the 1st literal in your string1 does not match with string2 answer is set to 0, after that even if you find the substring you are not changing answer to 1 so you are not getting the proper result.

Also you are just incrementing str2 index and not incrementing str1 index with it, so you will never find the string, so change the logic.

I rewrite some of your code:

#include <stdio.h>

int string_part_of_other(void)
{
   char str1[20];
   char str2[20];
   int answer = 0;
   int i, j, k;

   printf("Enter string 1:\n");
   scanf("%s", str1);

   printf("Enter string 2:\n");
   scanf("%s", str2);

   i = j = k =0;

   while(str2[j] !='\0')
   {
       k = j;
       for(i = 0; str1[i] != '\0' && str2[k] != '\0'; i++, k++)
       {
           if(str1[i] != str2[k])
           {
               break;
           }
       }

       if(str1[i] == '\0')
       {
           answer = 1;
       }
       j++;

   }

   return answer;
}

int main()
{
   int result;
   result = string_part_of_other();

   if (result == 1)
   {
      printf("Yes, string 1 is part of string 2.\n");
   }

   if (result == 0)
   {
      printf("No, string 1 is not part of string 2.\n");
   }

   return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM