Friday, November 21, 2014

C String pro



Program to sort set of strings in alphabetical order


#include
#include
void main()
{
   char s[5][20], t[20];
   int i, j;
   clrscr();
   printf("\nEnter any five strings : ");
   for (i = 0; i < 5; i++)
      scanf("%s", s[i]);
    for (i = 1; i < 5; i++)
    {
      for (j = 1; j < 5; j++)
      {
         if (strcmp(s[j - 1], s[j]) > 0)
         {
            strcpy(t, s[j - 1]);
            strcpy(s[j - 1], s[j]);
            strcpy(s[j], t);
         }
      }
   }
    printf("\nStrings in order are : ");
   for (i = 0; i < 5; i++)
      printf("\n%s", s[i]);
      getch();
}

Output
Enter any five strings :
pri
pra
pru
pry
prn
Strings in order are :
pra
pri
prn
pru
pry

Reverse Letter in Each Word of the Entered String

#include
#include
#include
void main()
{
   char msg[] = "Welcome to Programming World";
   char str[10];
   int i = 0, j = 0;
   clrscr();
    while (msg[i] != '\0')
     {
      if (msg[i] != ' ')
       {
         str[j] = msg[i];
         j++;
         }
        else
        {
         str[j] = '\0';
         printf("%s", strrev(str));
         printf(" ");
         j = 0;
      }
      i++;
   }
    str[j] = '\0';
   printf("%s", strrev(str));
   getch();
}

Output
emocleW ot gnimmargorP dlroW

To Encode Entered String and Display Encoded String


#include
#include
 char* encode(char* str)
{
   int i = 0;
   while (str[i] != '\0')
   {
      str[i] = str[i] - 30;  
      i++;
   }
   return (str);
}
void main()
{
           char *str;
             printf("\nEnter the String to be Encode : ");
   gets(str);
    str = encode(str);
   printf("\nEncoded String : %s", str);
   getch();
}

Output
Enter the String to be Encode : white
Encoded String : Yjkvg
Enter the String to be Encode : roses
Encoded String : Tqugu
Enter the String to be Encode : Japan
Encoded String : ,Crcp
Enter the String to be Encode : zebra
Encoded String : Gdtc

No comments:

Post a Comment