1
I am doubtful in the following exercise:
Write a C program that receives two strings via standard input and whether the second string is contained in the first string, i.e., if the second string is a segment of the first. You can consider, in your program, that the second string is always smaller than the first, and that the strings have at most 100 characters. Follow an example of input and output. What is underlined has been provided by the user.
- String 1: program
- String 2: grass
The second string is contained in the first!
The code I made always says that "The second string is contained in the first". Follows the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char**argv){
char string1[101];
char string2[101];
int i, x;
printf("Forneca a string 1: \n");
scanf("%s",string1);
printf("Forneca a string 2: \n");
scanf("%s",string2);
x=strlen(string2);
for(i=0; i<x; i++){
if(string2[i]=string1[i]){
printf("A segunda string esta contida na primeira. \n");
break;
}
}
return 0;
}
What’s not working out?
– Thiago Lunardi
to)
=
would be==
; b)scanf("%s",string1)
only reads a word (ends in the first space...– JJoao