Here is just a simple program to sort two strings inputted from the user entered on the command line.
A C Program to sort two strings
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAXBUFF 80
#define TRASHSIZE 200
/* Gets extra junk off stdin of size of TRASHSIZE */
void getJunk(void) {
char junk[TRASHSIZE];
fgets(junk,TRASHSIZE,stdin);
}
/* Converts upper case letters to lower case */
void toLower(char *str) {
int len = strlen(str), i;
for (i=0;i<len;i++)
if (str[i] >= 65 && str[i] <= 90)
name[i] += 32;
}
/* Has the same effect as strcmp but I just decided to write my own and save compiler time */
int cmpstr(char *, char*);
int main() {
char str1[MAXBUFF], str2[MAXBUFF];
int len1, len2, i;
/* Get the two strings from the user */
printf("Please enter a string: ");
scanf("%s", str1);
getJunk();
printf("Please enter another string: ");
scanf("%s", str2);
/* Convert both string to lowercase before comparing */
toLower(str1);
toLower(str2);
i = cmpstr(str1, str2);
if (!i)
printf("%s is equal to %s", str1, str2);
else if (i==1)
printf("%s %s", str1, str2);
else if (i==2)
printf("%s %s", str2, str1);
system("pause");
return 0;
}
int cmpstr(char *str1, char *str2) {
int len1, len2, i;
len1 = strlen(str1);
len2 = strlen(str2);
for (i=0;i<len1 && i<len2;i++) {
if (str1[i] < str2[i])
return 1;
else if (str[i] > str2[i])
return 2;
}
return 0;
}
John A 1,896 Vampirical Lurker Team Colleague
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.