sorting strings

This is a discussion on sorting strings within the C Programming forums, part of the General Programming Boards category; Code: // string sorting #include<stdio.h> #include<string.h> int main(){ char name[5][10]={"suresh","chanaka","sarith","Adnan","queen"}; char temp[10]; int k,i,min; for(i=0;i<4;i++){ min=i; for(k=i+1;k<5;k++){ if(strcmp(name[k],name[min])<0) min=k; } ...

  1. #1
    Registered User sureshhewa's Avatar
    Join Date
    May 2008
    Location
    sri lanka
    Posts
    13

    Post sorting strings

    Code:
    // string sorting
    
    #include<stdio.h>
    #include<string.h>
    
    int main(){
    
    char name[5][10]={"suresh","chanaka","sarith","Adnan","queen"};
    char temp[10];
    
    int k,i,min;
    
    for(i=0;i<4;i++){
    	min=i;
    
    	for(k=i+1;k<5;k++){
    		if(strcmp(name[k],name[min])<0)
    		min=k;
    	}
    
    	temp=name[i];              // error
    	name[i]=name[min];     // error
    	name[min]=temp;         //error
    
    }
    
    for(i=0;i<5;i++){
    printf("%s\n",name[i]);
    }
    
    return 0;
    }
    Please tell me where i have go wrong. 3 compile errors called "Lvalue required"

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You cannot assign into arrays.

    If you want to move strings around, use strcpy.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Code:
    	temp=name[i];              // error
    	name[i]=name[min];     // error
    	name[min]=temp;         //error
    Code:
    	temp[i]=name[i];            
    	name[i]=name[min];  
    	name[min]=temp[i];

  4. #4
    Registered User sureshhewa's Avatar
    Join Date
    May 2008
    Location
    sri lanka
    Posts
    13
    i tried both ways , didn't work

  5. #5
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Code:
                    temp[i]=name[i][k];            
    	name[i][k]=name[min][k];  
    	name[min][k]=temp[i];
    Try this, although 'k" may not be the correct subscript to use.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    The use of strcpy() should work.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User sureshhewa's Avatar
    Join Date
    May 2008
    Location
    sri lanka
    Posts
    13
    Code:
    strcpy(temp,name[i]);
    	strcpy(name[i],name[min]);
    	strcpy(name[min],temp[0]);
    you meant like this??

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by sureshhewa View Post
    Code:
    strcpy(temp,name[i]);
    	strcpy(name[i],name[min]);
    	strcpy(name[min],temp[0]);
    you meant like this??
    Make the red bit go away.

  9. #9
    Registered User sureshhewa's Avatar
    Join Date
    May 2008
    Location
    sri lanka
    Posts
    13
    thank you soo much. Im little bit careless. thanks for eveyone

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem sorting an array of strings
    By Leojeen in forum C Programming
    Replies: 7
    Last Post: 05-07-2008, 09:02 PM
  2. strings, sorting
    By kocika73 in forum C Programming
    Replies: 4
    Last Post: 02-18-2006, 03:29 PM
  3. Replies: 2
    Last Post: 02-23-2004, 05:34 AM
  4. bubble sorting multiple strings
    By jibbles in forum C Programming
    Replies: 10
    Last Post: 10-11-2003, 11:48 PM
  5. Sorting strings to speed up search?
    By Mox in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2001, 12:17 PM

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