Thread: String Comparison

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    21

    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...

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    21
    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...

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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.

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    21
    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)

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Ahh, here's your problem:
    Code:
    string += i;
    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:
    Code:
    string++;

  8. #8
    Registered User
    Join Date
    Jul 2010
    Posts
    21
    Thank you so much, that was an error as stupid as hard to spot. Grazie!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. std::string comparison versus int comparison
    By leeor_net in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2009, 07:28 AM
  2. String Comparison
    By kpreston in forum C Programming
    Replies: 16
    Last Post: 10-20-2008, 07:43 PM
  3. string comparison
    By sj999 in forum C Programming
    Replies: 4
    Last Post: 05-23-2008, 03:10 AM
  4. String Comparison
    By Cdrwolfe in forum C++ Programming
    Replies: 8
    Last Post: 03-27-2006, 10:59 AM
  5. using a string comparison in C
    By mr. J in forum C Programming
    Replies: 4
    Last Post: 05-09-2003, 05:40 PM