Thread: strcmp

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    28

    strcmp

    I'm trying to understand strcmp and not having much luck with it. What Im trying to do is input and compare 7 character strings with strcmp, re-order them accordingly and print out the result. I realize that scanf isnt the best thing to use but for the purposes of my program it works. I tried to be as simplistic as possible, and documented everything.

    When I remove the compare two strings section, everything works. The input and output print correctly.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    #define N_STRINGS 7
    
    int main(void) 
    
    {
    	// MAIN VARIABLE INITIALIZATION//
    	int cnt;  //Intializes counter for display/accessing the array row
    	int result; // Gives strcmp a place to put its output
    	char w[N_STRINGS][100]; //Initializes the string array to the number of (N_STRINGS)
    	char tmp; // Initializes the temp variable to swap strings
    	// MAIN VARIABLES END
    
    	//INTRODUCTION//
    	printf("%s %d %s","This program will take", N_STRINGS,"lines of input and re-order them alphabetically\n"); 
    	for (cnt = 0; cnt < N_STRINGS; ++cnt)  // For statement to fill the array
    		if(w != '\n') //Continues to fill the buffer until newline is entered from the keyboard
    		{
    			printf ("Enter line number %d\n", cnt + 1); 
    			scanf("%s", w[cnt]);
    		}
    	
    	
    	printf("%s","The lines you entered were:\n"); //Prints each line of the array using the line variable
    	for (cnt = 0; cnt < N_STRINGS; ++cnt)
    		printf("w[%d] = %s\n", cnt, w[cnt]);
    				
            //COMPARE THE TWO STRINGS USING STRCMP//
    	for (cnt = 0; cnt < N_STRINGS; ++cnt)
    		{
    		result = strcmp(*w[cnt], *w[cnt + 1]);
    		if (result > 0)
    		{
    			tmp = *w[cnt];
    			*w[cnt] = *w[cnt + 1];
    			*w[cnt + 1] = tmp;
    		}
    					
    	}
    	//OUTPUT//
    	printf("%s\n","Here is your sorted output.");
    	printf("%s\n\n","Strings are sorted alphabetically");
    	for (cnt = 0; cnt < N_STRINGS; ++cnt)
    		printf("w[%d] = %s\n", cnt, w[cnt]);
    	return 0;
    	
    }
    The two things my code does is one, a white space character terminates the string and puts whatever follows the white space into the next row of the array. The second is strcmp doesnt seem to work as I have it, and documentation hasnt been very easy to find.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You need to pass pointers to strings to strcmp(). You are passing in actual chars.

    Also:
    Code:
    if(w != '\n')
    w is a pointer to a pointer. Comparing it to a char is not going to work.

    Get in the habit of reading the warnings your compiler is spitting out, it will often explain odd behavior that you experience from your program.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Then of course, there's the whole "can't assign entire arrays" problem:
    Code:
    if (result > 0)
    		{
    			tmp = *w[cnt];
    			*w[cnt] = *w[cnt + 1];
    			*w[cnt + 1] = tmp;
    		}
    This won't work. Use strcpy if you're trying to reorder your array of strings.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by bithub
    You need to pass pointers to strings to strcmp(). You are passing in actual chars.

    Also:
    Code:
    if(w != '\n')
    w is a pointer to a pointer. Comparing it to a char is not going to work.
    w in that context is the address of the first element of a two dimensional array, this is not the same as pointer to a pointer.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    My mistake. The point still stands though.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    28
    My compiler didnt give me any warnings about it. It compiled and ran as I posted it. How would I change my code so that I can take an input string from the keyboard until \n is reached and then input that into my 2 dimensional array? Im going to attempt re-working strcpy into my code and hope that solves the copying issue.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use something like fgets, which is covered in the FAQ section. One such entry is here.
    Use strcpy as I've already suggested to copy the string over.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    You're trying to store string into tmp which is not a char array.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    28
    Im going to use fgets on my own. But I'm supposed to use scanf to get the input. The only thing I can think of at the moment would be to utilize another temp variable to capture the input string and transfer that after the fact to my array, but that may or may not be a correct way to go about seperating testing for /n and inputting to the array.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    28
    quzah,

    I appreciate your help in jogging my thought process. I re-did it using a strcpy and for the moment left the scanf in place. Its not pretty but it works which is all I had to do. Now Im gonna go back and do it the right way. Here is the code I finished with:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    #define N_STRINGS 7
    
    int main(void) //Main program
    
    {
    	// MAIN VARIABLE INITIALIZATION
    	int cnt = 0;  //Intializes counter for display/accessing the array row
    	int result; // Gives strcmp a place to put its integer output
    	int j = N_STRINGS - 1; //Decremental counter and compare variable
    	char w[N_STRINGS][100]; //Initializes the string array to the number of (N_STRINGS)
    	char tmp[100]; // Initializes the temp array to swap strings
    	// MAIN VARIABLES END
    
    	//INTRODUCTION
    	printf("%s %d %s","This program will take", N_STRINGS,"lines of input and re-order them alphabetically\n"); 
    	for (cnt = 0; cnt < N_STRINGS; ++cnt)  // For statement to fill the array
    		if (w != '\n') //Continues to fill the buffer until newline is entered from the keyboard
    		{
    			//scanf("%s", &tmp2); //New Code
    			//putchar(w[cnt]); //New Code
    			
    			printf ("Enter line number %d\n", cnt + 1); 
    			scanf("%s", w[cnt]); //Old Code
    		}
    	
    	//Prints each line of the array using the line variable
    	printf("%s","The lines you entered were:\n");
    	for (cnt = 0; cnt < N_STRINGS; ++cnt)
    		printf("w[%d] = %s\n", cnt, w[cnt]);
    	
    	//COMPARE THE TWO STRINGS
    	do{
    		for (cnt = 0; cnt < j; ++cnt)
    		{
    		result = strcmp(w[cnt], w[cnt +1]);
    		//DEBUG PRINTF STATEMENTS TO WATCH LOGIC IN PROGRESS
    		//printf("The result of %d strcmp is: %d\n", cnt, result);
    		//printf("This is string 1: %s\n", w[cnt]);
    		//printf("This is string 2: %s\n", w[cnt + 1]);
    		//DEBUG PRINTF STATEMENTS END
    			if (result > 0)
    			{
    				strcpy(tmp, w[cnt + 1]);
    				strcpy(w[cnt+1], w[cnt]);
    				strcpy(w[cnt], tmp);
    			}
    		}
    		--j;
    	}while	(j > 0);	
    
    	//Output
    	printf("%s\n","Here is your sorted output.");
    	printf("%s\n\n","Strings are sorted alphabetically");
    	for (cnt = 0; cnt < N_STRINGS; ++cnt)
    		printf("w[%d] = %s\n", cnt, w[cnt]);
    	return 0;
    	
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fucntion returns -1, Why?
    By Taper in forum C Programming
    Replies: 16
    Last Post: 12-08-2008, 06:30 PM
  2. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 04:02 PM
  3. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  4. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 08:23 PM
  5. strcmp, any borders?
    By demonus in forum C Programming
    Replies: 3
    Last Post: 11-15-2002, 02:48 AM