Solved

How to sort names alphabetically in C

Posted on 1997-12-11
5
3,349 Views
Last Modified: 2012-06-27
Hello, I'm a freshman programming student and I need a VERY FAST solution to a finals project our professor gave us. We need to write the code for a C program that:
1)Will ask the user to input names or words (perhaps 10 names)
2)Will show a menu asking the user if he/she wants the names to be arranged in ascending or descending alphabetical order
3)Will output the names in the order specified by the user in one column
I know this sounds dumb to experts like you people, but please help me.
I'm pulling my hair out just thinking about the solution.
THANKS VERY MUCH IN ADVANCE!!!!!
0
Comment
Question by:mamoru120997
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 7

Expert Comment

by:galkin
ID: 1256624
You ca use the fact that each letter is ASCII character, which is in turn no more that interger value. For instance a is 32, if I am not mistaken, b - 33 etc. So you can get first character in the word substruct 32 and sort numbers.  
0
 

Expert Comment

by:hbadr
ID: 1256625
I have the code for you ,if you want ,by the way 32 is the ascii code for (space)
0
 

Author Comment

by:mamoru120997
ID: 1256626
Not very useful.
0
 

Accepted Solution

by:
hbadr earned 200 total points
ID: 1256627
//Here is the code mamoru :

#include <stdio.h>
#include <conio.h>
#include <alloc.h>
#include <String.h>

void main()
{
 char **Ptr;
 int i,j;
 int size,sort;
 char swap[20];

 printf("How Much words do You Want To Sort: ");
 scanf("%d" ,&size );

 printf("\nChoose:");
 printf("\n1 - Assending");
 printf("\n2 - Dessending");

 sort=3;
 while( sort!=1 && sort!=2 ){
 printf("\nEnter Choice : ");
 scanf("%d", &sort);
 }

 Ptr = calloc( size , sizeof( char * ) );  // Dynamically allocate String
                                           // pointers
 for( i=0 ; i<size ; i++ ){
     Ptr[i] = calloc( 20 ,sizeof( char ) ); // Allocate 20 bytes for each
 }                                         // string

 for( i=0 ; i<size ; i++ ){                    // scan strings
     printf("\nEnter Word No. %d : " ,i+1 );
     scanf("%s", Ptr[i] );
 }

 if ( sort==1  ){
     for( j=0 ; j<size ; j++ ){       // sort assending
       for( i=j+1 ; i<size ; i++ ){
           if( Ptr[j][0] > Ptr[i][0] ){
               strcpy( swap,Ptr[i] );
               strcpy( Ptr[i] , Ptr[j] );
               strcpy( Ptr[j] , swap );
             }
         }
     }
 }
 if ( sort==2  ){
     for( j=size-1 ; j>=0 ; j-- ){     //sort dessending
         for( i=j-1 ; i>=0 ; i-- ){
           if( Ptr[j][0] > Ptr[i][0] ){
               strcpy( swap,Ptr[i] );
               strcpy( Ptr[i] , Ptr[j] );
               strcpy( Ptr[j] , swap );
             }
         }
     }
  }

 printf("\nSorted Words");
 for(i=0;i<size;i++){
     printf("\nWord No.%d : %s",i+1,Ptr[i]);
 }


}

0
 

Expert Comment

by:hbadr
ID: 1256628
SO why do you ask if don't need the answer ,these things take time if you may know.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question