-
String Comparison
Hi all, italian newbie here. Sorry for my bad english and for my "bad C", I'm trying to learn both.
The program I'm trying to write compare two string and define if the second one is included in the first one.
Code:
#include <stdio.h>
#include <string.h>
#define FRASE 10
void controlloStringhe (char* string1, char* string2, int dimensione, int parolaIntera);
int main()
{
char string1 [FRASE];
char string2 [FRASE];
printf("Inserire le due parole da controllare.\n");
scanf("%s %s", string1, string2); //read the two string without checking their lenght, i want to do this later by myself, that's not the actual problem
printf("Controllo se la seconda è contenuta nella prima...\n");
controlloStringhe(string1,string2,strlen(string2)+1,strlen(string1)+1);
return 0;
}
void controlloStringhe(char* string1, char* string2, int dimensione, int parolaIntera){
int i;
for(i=0;i<parolaIntera;i++){
string1+=i;
if(strncmp(string1,string2,dimensione)==0){
printf("La seconda stringa è contenuta nella prima.\n"); //the second one is included in the first one
return;
}
}
printf("La seconda stringa non è contenuta nella prima.\n"); //the second one is not included in the first one
}
e.g: if I write HELLO and ELL, the program should tell me that the second string is included in the first... while HLLO is not.
now, trying some word, that's the answers:
funzione funzione
the second is included
funzione unzione
the second is included
funzione zione
the second is included
funzione funzion
the second is NOT included
funzione ciao
the second is not included
funzione unzion
the second is not included
funzione nzione
the second is not included
it is clear to me that there is some problem, don't know where...
-
Check the order of these parameters, and get rid of the +1's.
Code:
controlloStringhe(string1,string2,strlen(string2)+1,strlen(string1)+1);
Also, you could use strstr() to shorten your code.
-
There's a function in string.h called strstr. It does exactly this for you.
If you still want to do it yourself, you need to remove the +1 from the strlen(string2) and strlen(string1) in the call to controlloStringhe. That includes the null character in the lengths, which you don't want to do. strncmp will handle that automatically.
-
I've deleted the +1's, i thought the NULL character was necessary... but it still doesn't work. The order of the parameters in the call of the function is correct.
I don't want to use the strstr function, because it is the target of my excercise...
-
It worked for me, all I did was remove the +1. No need to switch the order of the arguments. Post your new code and what input you gave that didn't work.
EDIT: NULL is the null pointer constant (like saying "this pointer points to nothing"). The null character, '\0', is what is used to end a string. The string needs the null character in it (scanf puts it in there), but you don't have to account for it when you pass in the string length.
-
Code:
#include <stdio.h>
#include <string.h>
#define FRASE 10
void controlloStringhe (char* string1, char* string2, int dimensione, int parolaIntera);
int main()
{
char string1 [FRASE];
char string2 [FRASE];
printf("Inserire le due parole da controllare.\n");
scanf("%s %s", string1, string2);
printf("Controllo se la seconda è contenuta nella prima...\n");
controlloStringhe(string1,string2,strlen(string2),strlen(string1));
return 0;
}
void controlloStringhe(char* string1, char* string2, int dimensione, int parolaIntera){
int i;
for(i=0;i<parolaIntera;i++){
string1+=i;
if(strncmp(string1,string2,dimensione)==0){
printf("La seconda stringa è contenuta nella prima.\n");
return;
}
}
printf("La seconda stringa non è contenuta nella prima.\n");
}
e.g.
function n (doesn't work)
function tion (doesn't work)
anduril duril (doesn't work)
anduril duri (doesen't work)
-
Ahh, here's your problem:
The first time through, i == 0, so you get "function". The second time through, i == 1, you you get "unction". Then i == 2, and you go forward two letters, getting "ction", then i == 3 and you get "on", then i == 4 and you go off the end of the word. Just increment string by 1 each time:
-
Thank you so much, that was an error as stupid as hard to spot. Grazie!