Doubt in the search for sub-string, inside a string

Asked

Viewed 90 times

0

I did everything the question asked only that I’m taking 70% error.

Question link

My code

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char** argv)
{
   char nome[10000], zelda[6] = "zelda", *ponteiro;
   ponteiro = NULL;
   int i;
   getchar();
   scanf("%[^\n]", nome);
   for(i=0;i<strlen(nome);i++)
   {
      nome[i]=tolower(nome[i]);  //convertendo tudo para minusculo
   }
   ponteiro = strstr(nome, zelda);
   if(ponteiro)
   {
      printf("Link Bolado\n");
      ponteiro = NULL;
   }
   else
   {
      printf("Link Tranquilo\n");
      ponteiro = NULL;
   }

    return 0;
}
  • @Pedro "(...) the problem is very clear when he specifies that the string to be validated is "Zelda". Strings like "Zelda", "Zelda", etc are invalid (...)" Is it ? The one example of a string in the sample outputs is "Zeldao" - "Link Bolado", in which the Z is uppercase. And the order of the parameters in strstr is correct. The function searches str2 in str1. Of documentation "str1 - C string to be Scanned."

1 answer

1

I see two problems: the first is the size of the array you defined as:

char name[10000]

When the question says that the input can be S(1 |S| 10 5), that is, a string of size 1 up to 100000 (one hundred thousand), as C strings need character 0 at the end of the array, then you should set your array like this:

char name[100001]; /* maximum size one hundred thousand and one */

The other problem is getchar() before scanf(), it is not necessary because with this you always remove the first character of the entry making an entry that starts with "Zelda" is "Elda" and making the answer "Quiet Link" when it is actually "Bolado Link".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.