You are on page 1of 4

#include <stdio.h> #include <stdlib.h> #include <string.

h> #define N 5 #define MAX_STRLEN 80 void sort(char **a, int n); char *stringArray[N]; int main(int argc, char *argv[]) { int i; printf("\nEnter %d names, one per line:\n",N); for (i = 0; i < N; i++) { stringArray[i] = (char *)malloc(MAX_STRLEN); strcpy(stringArray[i],""); printf("> "); fgets(stringArray[i],MAX_STRLEN,stdin); *strchr(stringArray[i],'\n') = '\0'; } sort(stringArray,N); printf("\nSorted:\n"); for (i = 0; i < N; i++) { puts(stringArray[i]); free(stringArray[i]); } return 0; } /* selection sort */ void sort(char **a, int n) { int min,i,j; char t[MAX_STRLEN]; for (i = 0; i < n; i++) { min = i; for (j = i+1; j < n; j++) { if (strcmp(a[j],a[min]) < 0) min = j; } strcpy(t,a[min]); strcpy(a[min],a[i]); strcpy(a[i],t); } }

#include <stdio.h> #include <string.h> main()

{ int i,j,n; char a[10][20],t[20]; printf(Enter the number of strings :); scanf(%d,&n); for(i=0;i<n;i++) scanf(%s,a[i]);// read the strings for(i=0;i<n-1;i++) //bubble sort for(j=0;j<n-1-i;j++) if(strcmp(a[j],a[j+1])>0) { strcpy(t,a[j]); strcpy(a[j],a[j+1]); strcpy(a[j+1],t); } printf(The strings after sorting are : n); for(i=0;i<n;i++) { printf( %s,a[i]);// print the strings printf(n); } } #include<stdio.h>
#include<string.h> #include<conio.h> void main() { char a[30],temp; int n=0,j,i; clrscr(); printf("Enter the string\n"); gets(a); while(a[n]!='\0') { n++; } for(i=0;i<n;i++) { for(j=0;j<n-i-1;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } printf("The string in alphabetical order is\n"); for(i=0;i<n;i++) { printf("%c",a[i]); } getch(); }

#include<stdio.h> #include<string.h> #include<stdlib.h> void reorder(int n, char *x[]); int main(){ int i , no_string; char *x[10]; printf("Enter the no of strings: "); scanf("%d",&no_string); printf("Enter the strings: \n"); for(i = 0; i < no_string; i++){ x[i] = (char*) malloc (12*sizeof(char)); printf("string %d: ",i+1); scanf("%s", x[i]); } printf("The sorted strings are: "); reorder(i,x); for(i = 0; i < no_string; i++){ printf("\nstring %d: %s", i+1, x[i]); } return 0; } void reorder(int n, char *x[]){ char *temp; int i,item; for(item = 0; item < n-1; ++item){ for(i = item+1; i < n; ++i){ if(strcmp(x[item],x[i]) > 0){ temp = x[item]; x[item] = x[i]; x[i] = temp; } } } return; }

#include <stdio.h> #include <stdlib.h> #include <string.h> #define WS " ,\t!:;.-" #define MAX_STR_LEN 1024 #define MAX_NUM_WORDS 1024 void selectionSortCstr(char **a, size_t rows); void printWordArray(char **a, size_t rows); char line[MAX_STR_LEN]; char *words[MAX_NUM_WORDS]; int main(int argc, char *argv[]) { size_t count; char *s; while (1) { count = 0; printf("\nEnter words to sort: \n> "); fgets(line,MAX_STR_LEN,stdin); *(strchr(line,'\n')) = '\0'; for (s = strtok(line,WS); s != NULL; s = strtok(NULL,WS)) { words[count++] = s; } selectionSortCstr(words,count); printf("\nsorted:\n"); printWordArray(words,count); } return 0; } void selectionSortCstr(char **a, size_t rows) { int i, j, min; char *t; for (i = 0; i < rows; i++) { min = i; for (j = i+1; j < rows; j++) { if (strcmp(a[j],a[min]) < 0) { min = j; } } t = a[min]; a[min] = a[i]; a[i] = t; } } void printWordArray(char **a, size_t rows) { size_t i; for (i = 0; i < rows; i++) { printf("%s\n",a[i]); } }

sort ten names in descending order


}
#include<stdio.h> int main(){ int i,j,n; char str[20][20],temp[20]; puts("Enter the no. of string to be sorted"); scanf("%d",&n); for(i=0;i<=n;i++) gets(str[i]); for(i=0;i<=n;i++) for(j=i+1;j<=n;j++){ if(strcmp(str[i],str[j])>0){ strcpy(temp,str[i]); strcpy(str[i],str[j]); strcpy(str[j],temp); } } printf("The sorted string\n"); for(i=0;i<=n;i++) puts(str[i]); return 0; }

strcpy(s[j-1],s[j]); strcpy(s[j],t); } } printf("Strings in order are : "); for(i=0;i<5;i++) printf("\n%s",s[i]); getch(); }


W a P to accept 5 names from user & store these names in array. sort these array elements in alphabetical order. #include #include #include main() { int i,j,d; char ch[20][30],temp[30]; clrscr(); printf(How many strings to sort ::); scanf(%d,&d); printf(nenter %d strings,d); for(i=0;i<=d;i++) gets(ch[i]); printf("nn"); printf("U hav enterd :::n"); for(i=0;i<=d;i++) printf("t%s",ch[i]); for(i=0;i<=d;i++) { for(j=i+1;j<=d;j++) { if(strcmp(ch[i],ch[j])>0) { strcpy(temp,ch[i]); strcpy(ch[i],ch[j]); strcpy(ch[j],temp); } } } printf(nn); printf(*******************nn); printf(Alphabetical sorted array is:::nn); for(i=0;i<=d;i++) printf(t%s,ch[i]); getch(); }

Program to sort set of strings in alphabetical order


#include<stdio.h> #include<string.h> void main() { char s[5][20],t[20]; int i,j; clrscr(); printf("Enter any five strings : \n"); 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]);