Skip to main content

C-Programming Program 6(c)


6( c) Given an Array of Strings Write a Program to Sort the String in Dictionary Order.

Algorithm


Sorting of string

1. Start
2. Read n
3. Initialize i:=0
4. Repeat steps 4 to 6 if i<n. else go to step 7
5. Read name[i]
6. i:=i+1
7. Set i:=0
8. Repeat steps 8 to 16 if i<n. else go to step 17
9. Set j:=i+1
10. Repeat steps 10 to15 if j<n. else go to step 16
11. If strcmp(name[i],name[j])>0 then go to step 12. else go to step 15
12. strcpy(temp, name[i])
13. strcpy(name[i],name[j])
14. strcpy(name[j],temp)
15. j:=j+1
16. i:=i+1
17. Initialize i:=0
18. Repeat steps 18 to 20 if i< n. else go to step 21
19. Write name[i]
20. i:=i+1
21. Stop

Flowchart:




Program

#include<stdio.h>

#include<string.h>
main()
{
 char names[50][20], temp[20];
 int n,i,j;
 printf("\n How many names U want:");
 scanf("%d", &n);
 printf("\n Enter the %d names one by one \n",n);
 for(i=0; i<n; i++)
      scanf("%s", names[i]);
 for(i=0; i<n-1; i++)
   {
      for(j=i+1; j<n; j++)
         {
             if(strcmp(names[ i ], names[ j ]) > 0)
               {
        strcpy(temp, names[ i ]);
strcpy(names[ i ], names[ j ]);
strcpy(names[ j ] , temp);
       }
         }
    }
printf("\n The Names in Dictionary Order:");
  for(i=0; i<n; i++)
      printf("\n %s", names[ i ]);
}

Comments

Popular posts from this blog

C-Programming with Record

C-Programming Programs Syllabus Click for full record  1.a) Write a C program to ask the user to enter one char ( Upper-Case letter) check  whether user  entered a Upper-case letter or not(by using relational and logical operators) and then if user has  entered a Upper-case letter  convert into a Lower-case letter? ( hint: Upper-case means capital letters,  use ASCII information to check for Upper-case and convert) b) Write a  C program to ask the user to enter two integers and apply all arithmetic operations on those  print the corresponding values?(hint : +,-,*,/,%)     c) Write a C program to Determine the ranges of char, short, int and long int variables both signed and  Unsigned   By using sizeof operator(ii) By printing appropriate values from standard header (limits.h )   2.a) Write a Program to Find the Roots of a Quadratic Equation using if else and Switch statements. b) Write a Program which Gener...

OOPS CPP Program-14

14. Write a program to find transpose of a given matrix of mxn size using unary operator overloading #include<iostream> using namespace std; class matrix {         int x[5][5],m,n; public:         matrix(){ }         matrix(int a,int b)         {                m=a;n=b;         }         void get();         void put();         matrix operator !(); }; void matrix::get() {         int i,j;         cout<<"\n Enter a matrix of order"<<m<<"x"<<n<<"\n";                for(i=0;i<m;i++)                    for(j=0;j<n;j++)                        ...

OOPS CPP Program-15

15. Write a program to concatenate one string to another using binary operator overloading. #include<iostream> #include<string.h> using namespace std; class str  {         char *st; public:         str(int len, char *s)         {                  int l=strlen(s);                st= new char[l];                st=s;          }         str()         {         }         void put();         str operator +(str); }; str str :: operator +(str s)  {  /*str abc; abc.st=strcat(st, s.st); return abc;*/         st=strcat(st, s.st);         return *this; } void str :: put() {   cout<<...