help with #define, strcmp, and strcpy

This is a discussion on help with #define, strcmp, and strcpy within the C Programming forums, part of the General Programming Boards category; Hello everyone, I am having a hard time making this program work. Request assistance Code: #include <stdio.h> #include <string.h> /* ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    4

    Smile help with #define, strcmp, and strcpy

    Hello everyone,
    I am having a hard time making this program work.
    Request assistance

    Code:
    #include <stdio.h>
    #include <string.h>
    
    /* I also need to use this in the program */
    /* #define N_STRINGS 7 */
    
    	int main(void)
    {
    	int j;
    	int i;
    	char str[8];
    	char temp[7];
    
    	/* Enter the 7 characters to form a string */
        printf("Enter up to 7 characters: ");
        scanf(" %s",str);
    
    	/* Print the unsorted strings in a list */
    	for (i = 0; i < 7; i++)
    	{
            printf("str[%d] = %c\n", i, str[i]);
    	}
    	printf("\n");
    
    	/* Sort the strings */
    
    	for (i = 0; i < 7; i++)
    	{
    		for (j = i + 1; j < 7; j++)
    		{
    			if (strcmp(&str[i],&str[j])<0)
    			{
    				strcpy(temp,&str[i]);
    				strcpy(&str[i],&str[j]);
    				strcpy(&str[j],temp);
                }
    		}
    	}
    
    	/* Print the sorted list */
        printf("\nSorted strings are:\n\n");
        for (i = 0; i < 7; i++)
            {
                printf("String[%d] = %c\n", i, str[i]);
                }
    	return 0;
            }

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    both i and j have to be to 7-1 or 6 because you acces i+1 and j+1, if you didn't do 6 or 7-1 then you have a array subscript out of bounds. To swap something you want it to be like this
    Code:
    char buffer;/*doesn't need to hold the entire string only one character*/
    buffer=str[i];
    str[i]=str[j];
    str[j]=buffer;
    you only use strcpy to copy strings not characters. for the define uncomment it and replace the 7 with N_STRINGS

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    4

    Smile

    Let me try to understand. i & j has to be the same value or it will
    be OFB. So, if I change the j to 8 and leave i along will that be okay?
    I'm looking at the other information.
    Thanks

    Code:
    /* To */
    for (j = i + 1; j < 8; j++) 
    
    
    /* From */
    for (j = i + 1; j < 7; j++)

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    4

    Smile

    I mess up. on my last reply.
    sorry

  5. #5
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    something like this
    Code:
    #include <stdio.h>
    #define N_STRINGS 7
    
    int main(void){
         int j;
         int i;
         char str[N_STRINGS+1]={'0'};
         char temp;
         printf("Enter up to %d characters: ",N_STRINGS-1);
         scanf(" %s",str);
         for (i = 0; i < N_STRINGS; i++)
               printf("str[%d] = %c\n", i, str[i]);
         printf("\n");
         /* Sort the strings */
        for(i=0;i<N_STRINGS-1;i++){
            for(j=0;j<N_STRINGS-1;j++){
                 if(str[j]>str[j+1]){
                    temp=str[j];
                    str[j]=str[j+1];
                    str[j+1]=temp;
                  }
              }
        }
        printf("\nSorted strings are:\n\n");
        for (i = 0; i < N_STRINGS; i++){
              printf("String[%d] = %c\n", i, str[i]);
         }
         return 0;
    }
    Last edited by linuxdude; 11-20-2004 at 11:19 PM.

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    4
    Thank you very much for the help I see now differ people have differ ways about writing c language. I like the way you go about it.
    http://cboard.cprogramming.com/newre...te=1&p=414374#
    Big Grin
    Code:
    #include <stdio.h>
    #include <string.h>
    #define N_STRINGS 7
    
    int main(void)
    {
    	int j;
    	int i;
    	char str[N_STRINGS + 2] = {'0'};
    	char temp;
    
    	/* Enter the 7 characters to form a string */
        printf("Enter up to %d characters: ",N_STRINGS);
        scanf(" %s",str);
    
    	/* Print the unsorted strings in a list */
    	for (i = 0; i < N_STRINGS; i++)
    	{
            printf("str[%d] = %c\n", i, str[i]);
    	}
    	printf("\n");
    
    	/* Sort the strings */
    	for (i = 0; i < N_STRINGS; i++)
    	{
    		for (j = i + 1; j < N_STRINGS; j++)
    		{
    			if (strcmp(&str[j],&str[i])<0)
    			{
    				temp = str[i];
    				str[i] = str[j];
    				str[j] = temp;
                }
    		}
    	}
    
    	/* Print the sorted list */
        printf("\nSorted strings are:\n\n");
        for (i = 0; i < N_STRINGS; i++)
            {
                printf("String[%d] = %c\n", i, str[i]);
                }
    	return 0;
            }

  7. #7
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Here's a godly sorter that sorts input from 0-255 characters long! The trick is to use strlen() instead of having a set # of input characters.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
    	int j;
    	int i;
    	char orig[255];
    	char temp;
    	
    	/// Get input
    	printf("Any characters you want & I shall order them:\n");
    	fgets(orig, sizeof(orig), stdin);
    	
    	/// Echo out original
    	printf("IN: %s\n", orig);
    	
    	/// Sort
    	for(i = 0; i < strlen(orig); i++) {
    		for(j = i; j < strlen(orig); j++) {
    			if(strcmp(&orig[i],&orig[j])>=0) {
    				temp = orig[i];
    				orig[i] = orig[j];
    				orig[j] = temp;
    			}
    		}
    	}
    
    	/// Echo out sorted
    	printf("\nSorted strings are:\n\n");
    	for(i = 0; i < strlen(orig); i++) {
    		if(orig[i] == '\n')
    			continue;
    		printf("String[%d] = %c\n", i, orig[i]);
    	}
    		
    	return 0;
    }
    Here's the an example:
    Code:
    Any characters you want & I shall order them:
    987239427347382882
    IN: 987239427347382882
    
    
    Sorted strings are:
    
    String[1] = 2
    String[2] = 2
    String[3] = 2
    String[4] = 2
    String[5] = 3
    String[6] = 3
    String[7] = 3
    String[8] = 4
    String[9] = 4
    String[10] = 7
    String[11] = 7
    String[12] = 7
    String[13] = 8
    String[14] = 8
    String[15] = 8
    String[16] = 8
    String[17] = 9
    String[18] = 9
    Last edited by Kleid-0; 12-20-2004 at 08:40 PM. Reason: I forgot an example!

  8. #8
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    please don't bump old threads. I didn't even remember this thread. I was wondering who posted on my name(I thought it was my brother)

  9. #9
    ---
    Join Date
    May 2004
    Posts
    1,379
    *slaps Kleid-0 in the back of the head again*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 09:42 AM
  2. strcmp and strcpy error
    By behzad_shabani in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2008, 05:15 PM
  3. 2D arrays, strcpy and strcmp Problems
    By Windu102 in forum C Programming
    Replies: 3
    Last Post: 08-23-2008, 02:00 AM
  4. strcpy & strcmp, what is wrong?
    By Silverdream in forum C Programming
    Replies: 7
    Last Post: 02-13-2002, 03:36 PM
  5. strcmp
    By coug2197 in forum C++ Programming
    Replies: 4
    Last Post: 02-01-2002, 12:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21