Dr. A.P.J. Abdul Kalam Technical University, Lucknow
KCS151 / KCS251 Programming for Problem Solving - Using C Language
Lab Exercises
31. Write a program to sort the array of characters (String) in alphabetical order like STRING in GINRST.
/*
File: Prgrm31.c
Author: Aditya Saini
Date: Jan 18, 2021
Description: Program to sort the array of characters (String) in alphabetical order like
STRING in GINRST.
*/
#include <stdio.h>
#include <math.h>
int main (void)
{
char str[20];
char key;
int len;
int i, j;
//Input string
printf ("Input String: ");
gets (str);
//Sorting
len = strlen (str);
for (i = 1; i <= len - 1; i++)
{
key = str[i];
for (j = i - 1; j >= 0 && str[j] > key; j--)
str[j + 1] = str[j];
str[j + 1] = key;
}
//Print new string after sorting
printf ("New string after sorting: %s", str);
};
Output
Input String: DIGITALG1
New string after sorting: 1ADGGIILT
No comments:
Post a Comment
Please do not post spam links.