Function that given a strings, prints another | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Function that given a strings, prints another

As the title i Need to build a function wich given a string, if It recognizes It, prints another estabilished string. I tried using the strstr function but If in the input there's the same string 2 times, i Will get only 1 output, instead of 2 as aspected. I know i should build some sort of FOR or While structure but i Just can't find the way. Thanks to everyone Will give a hand

12th Mar 2021, 10:39 AM
Cristopher Ricciotti
Cristopher Ricciotti - avatar
4 Answers
+ 1
from function description: The strstr() function finds the FIRST occurrence of the substring needle in the string haystack. The terminating null bytes ('\0') are not compared. I used strtok and strcmp for this task
12th Mar 2021, 11:34 AM
Igor Kostrikin
Igor Kostrikin - avatar
0
Here Is the code: #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char input[100]; fgets(input, 100, stdin); char *Lion; Lion = strstr(input, "Grr"); if (Lion){ printf("Lion\t"); } return 0; }
12th Mar 2021, 10:40 AM
Cristopher Ricciotti
Cristopher Ricciotti - avatar
0
Thanks for the tip Igor, but what if I wanted to write a function instead of using One given by the string.h header?
12th Mar 2021, 11:41 AM
Cristopher Ricciotti
Cristopher Ricciotti - avatar
0
You could try;)
13th Mar 2021, 2:15 PM
Igor Kostrikin
Igor Kostrikin - avatar