I need a program to sort the words of a string leixographically in C. For example : INPUT- how are you
OUTPUT: are how you
Printable View
I need a program to sort the words of a string leixographically in C. For example : INPUT- how are you
OUTPUT: are how you
Post your attempt.
Code:#include <stdio.h>
int main()
{
char arr[100];
int count =0;
int j,i,k;
char temp;
printf("enter the string\n");
gets(arr);
for(i=0;arr[i]!='\0';i++)
count++;
printf("value of count is %d\n",count);
for(k=0;k<=count;k++)
{
for(j=0;j<count-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
puts(arr);
return 0;
}
But its a futile effort.Its not working!!!
How does your compiler not warn you about using gets() > FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com
You'll want to replace the first loop (that simply gets the length) with something that actually counts words.
From there, many potential approaches are possible. For example, once you know the number of words, you can then split the string up into a temporary array of "words", and then copy them back into the array in order.
I did not test the above, but give it a shot. In C, you can't compare strings directly. You have to use strcmp() (or write your own code for that purpose).Code:#include <stdio.h>
#include <string.h> //for strcmp(), strcpy()
int main(void)
{
//char arr[100];
char words[100][50];{
{"It"},
{"would"},
{"be"},
{"as"},
{"easy"},
{"as"},
{"breathing"}
};
int count =0;
int j,i,k;
//char temp;
char temp[50]; //enough to hold one word
printf("enter the string\n");
//gets(arr);
//never use gets! Totally unsafe
//an example: fgets(arr[i], sizeof(arr[i], stdin);
//remember that fgets WILL add the \n onto the end of a string, if possible, so remove it before making any
//string comparisons with:
/*
int len = strlen(arr[i]);
if(arr[i][len]=='\n')
arr[i][len]='\0'; //overwrite the newline and shorten the string
Yes, it's a bit of a pain, but if you continue to use gets(), you're just writing code that any hacker can exploit with well
known attacks, to control the system, eventually.
*/
count = 7;
for(j=0;j<count-1;j++)
{
if((strcmp(arr[j], arr[j+1])) < 0)
{
strcpy(temp, arr[j]);
strcpy(arr[j], arr[j+1]);
strcpy(arr[j+1], temp);
}
}
for(i=0;i<count;i++)
puts(arr[i]);
return 0;
}
Thank you...but I don't understand one thing , why using gets() makes my program vulnerable to hackers?
Because gets does not provide a mechanism to specify the number of characters you want to read, therefore potentially overflowing your storage container and rewriting areas of memory that would jump execution to a sector of malware code maliciously inserted by a hacker, instead of your own code. This is what is called a buffer overflow attack and I am sure you can come across more literature on it on Google.
This is probably the best link out there explaining how the attack works..
well i think you would need to strtok each word using the strtok function in the header and then search for the beginning of each string ordering the items as you want and then print.