You are here:

C/rearrange string

Advertisement


Question
QUESTION: Hi Prince M. Premnath, It's me again, mazlee.

 Here I have one question in C programming that I blur to answer. Hope U can help me. The question as below :-

Assume that you are required to enter a list of strings into a computer, rearrange them in alphabetical order, and then print out the rearranged list. The strings must be stored in a two-dimensional character array. Each string will be stored in a separate row within the array. You may use any suitable string manipulation functions. One of the library functions that can be used is strcmpi.
strcmpi compares the strings but does not differentiates between upper and lowercase characters.  The function accepts two strings as arguments and returns integer value, depending on the relative order of the two strings, as follows:

a)    A negative value is returned if the first string alphabetically precedes the second    string.
b)   A value of zero is returned if the first string and the second string are identical.
c)   A positive value is returned if the second string alphabetically precedes the first    string.

If the function strcmpi (string1, string2) returns a positive value then, this indicates that string2 must be placed ahead of string1 in order to rearrange the two strings in alphabetical order.
Sample output is given as follows:

Enter each string on a different line:

Type ‘END’ when finished

String 1: Panda
String 2: Apple
String 3: Island
String 4: Calendar
String 5: Brown
String 6: Black
String 7: Rainbow
String 8: News
String 9: Ballroom
String 10: Canada
String 11: END

Reordered list of strings:

String 1: Apple
String 2: Ballroom
String 3: Black
String 4: Brown
String 5: Calendar
String 6: Canada
String 7: Island
String 8: News
String 9: Panda
String 10: Rainbow
String 11: END


ANSWER: Hi dear Mazlee !

Thanks for your comments , may i know your full name ?. ive very well understood your question, i kindly suggest you to refer this link
http://www.java2s.com/Tutorial/C/0060__String/Thefunctionalapproachtostringsorti

Bit of advanced code
http://www.cs.princeton.edu/~rs/strings/demo.c

Note: If you want to write code for your question , no probs ill do it , just gimme a follow up :-)

Thanks and Regards.
Prince M. Premnath.



---------- FOLLOW-UP ----------

QUESTION: Hi Prince M.Premnath,

 Thanks for your help. My name is Mazlee Mamat. I'm new in C programming and I want to learn from the expert like U :). The link given are useful but i need time to understand the step by step how it's works.

  If not to much problem to U can U show me a simple C programming to meet the condition above. Thanks a lot.

Answer
Hi dear Mazlee Mamat :)

 Ive programmed this simple programme exatctly matches your need using strncmp() function ( i know the earlier programme might ve confused you due to complex pointer usage )
have this simple programme


#include<stdio.h>
#include<conio.h>
#include<string.h>
char Names[10][20];
void main()
{
  char Temp[20];
  int i,j;
  int n = 5;
  for( i = 0 ; i <n; i++)
     gets(Names[i]);

  for( i = 0; i<n ; i++)
  {
     for(j=0; j< n-1; j++)
     {
        if( strncmp(Names[j], Names[j+1],1) > 0 )
        {
         strcpy(Temp,Names[j]);
         strcpy(Names[j],Names[j+1]);
         strcpy(Names[j+1],Temp);
        }
     }
  }

  for( i = 0 ; i< n ; i++)
     puts(Names[i]);
  getch();
}

Note; I havn't tested this programme , if you found any bugs please report to me ;) , make sure mark your questions as private if you wish to have in regular touch with me , get back to me if you have any doubts.

Feel free to text me

Thanks and Regards!
Prince M. Premnath

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Prince M. Premnath

Expertise


I'm sure that I can solve any doubts in Turbo C ,Graphics Programing ,Mouse, Hardware Programming ,File System ,Interrupts, BIOS handling , TSR Programming , General Concepts in C Language, handling inline Assembly statements

Experience

Research over 6+ Years

Organizations
CG-VAK Softwares and Exports Limited

Education/Credentials
Masters in Computer Applications

©2012 About.com, a part of The New York Times Company. All rights reserved.