how can i arranged the input words alphabetically, like in the dictionary..please help me....please!!!!

Recommended Answers

All 9 Replies

first intialize one string such as str[12]
take two variable of integer type such as i,j
take one temporary variable of char type such as temp

for(i=0;str[i]!='\0';i++)  /*loop will be continue untill the it not found the null charater  from str string*/
{
   for(j=1;str[j]!='\0';j++)
   {

          if(str[j]>str[i]) /*chack whether the next charater is smaller  than the cuurent charater */
         {
          temp=str[j];
         str[j]=str[i];
         str[i]=temp;
        }

   }
}

  for(i=0;str[i]!='\0';i++)  /*print the string */
  {
    printf("%c",str[i]);

  }

}

@nitu_thakkar

That appears to be a sort for the characters in a string, to put the highest character in the string as the first character.

@taichou

The above sort could be adapted to sort strings in a list.

Where do the words come from?

How do we know how many there are (or will be)?

Once we get them in dictionary order, do we just output the list?

you will input the words...and then after sorting, you will print them in an alphabetical order....tnx for the help..appriciate it...

i agree with death_oclock and what i type here is actually just more detailed explanation of his suggestion

problem is that if you use standard input (keyboard), you will have to make dynamic array, or to predetermine number of maximum words.
bubble sort is pretty good option since it's simple and easy to use, and you will input form keyboard which means there wont be too many entries.
if you know how to use more complex structures as dynamic arrays, then all you need to do is make one such array. dynamic array works in way that expands with each entry and you will know the length of your array. strcmp or strcmpl (same as previous but capital letters are considered the same as lower case letters) are function that compares two strings. strcmp (string1, string2) evaluates (equals) to 0 if two string are identical, evaluates less than 0 if first string is "lower" (alphabetically comes before second one), and evaluates more than 0 if first string is "higher" (goes after the second).
everything said here expects that you know how to work bubble sort, strings, and dynamic array thing is just optional. you can say that there will be no more than 100 words and you will spare yourself from dynamic arrays.

taichou, I provided links with the hope that you would read them and maybe learn from them.

how can i arranged the input words alphabetically, like in the dictionary..please help me....please!!!!

The simple program on alphabetical sorting is:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
 clrscr();
 char a[6][7];
 int i,j,p;
 char c[10];
 printf("enter the name:");
 for(i=0;i<6;i++)
 scanf("%s",&a[i]);
 printf("the names entered are:");
 for(i=0;i<6;i++)
  {
  printf("%s",a[i]);
  printf("\n");
 }
 for(i=0;i<6;i++)
  {
  for(j=i;j<6;j++)
   {
   p=strcmp(a[i],a[j+1]);
   if(p>0)
    {
    strcpy(c,a[i]);
    strcpy(a[i],a[j+1]);
    strcpy(a[j+1],c);
    }
   }
  }
 printf("After sorting:\n");
 for(i=0;i<6;i++)
 printf("%s\n",a[i]);
 getch();
 }

Your indentation could use some more noticeable spaces. #include<conio.h> is not a standard header file. clrscr(); and getch(); are not portable C standard functions. void main() should always return an int, except when there's not host operating system. printf("enter the name:"); you ask for a name, but expect six to be entered via a loop. scanf("%s",&a[i]); a is a pointer already. What if the input is more than seven which is the limit of char a[7]? scanf() should always be limited on reading and checked at return for failure. strcmp(a[i],a[j+1]); strcmp(a[5], a[5+1]); What's in a[6]? Oh, there's not an a[6]. Same it is applicable for strcpy()

thank you for correcting me.
we can use

int main()
{
----
----
return(0);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.