Hello,

I am writing a c program to Prompts the user (altogether 10 times) to enter a word not longer than 9 characters. And it needs the modification of each word by changing lower case to upper and then the reverse of the word is concatenated with the word.FInally , show them in unsorted way and sorted way.
After that, the program terminates. I have made many attempts at writing this program and this is where i am at right now and it still doesnt work. Thanks in advance for the input.

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

char toupper(char);
char tolower(char);

int main(void)
{

char words[10][19];
char c;
int i, j;
int k;
char swap;

for(i = 0; i < 10; i++)
{
printf("enter the character \n");
fflush(stdout);
for (j = 0; j < 10; j++)
{
words[i][j] =fgetc(stdin);
if (words[i][j] == '\n')
{
words[i][j] = '\0';
} /* end of if */

}
}


for(i = 0; i < 10; i++){
for (j = 0; j < 10; j++){
k = (int)(words[i][j]);
if (k > 64 && < 91)
{
words[i][j]= tolower(words[i][j] );
}

else if (k >96 && <123);
{
words[i][j]= toupper(words[i][j] );
}
}
}

printf("unsort \n");

for(i = 0; i < 10; i++){
printf("%s \n ", words[i]);
fflush(stdout);
}

printf("Sorted: \n");

for(i=9; i>0; i--) {
for(j=0; j<i; j++) {
if (strcmp(words[j],words[j+1])>0) {
strcpy(swap,words[j]);
strcpy(words[j],words[j+1]);
strcpy(words[j+1],swap);
}
}
printf("%s \n", words[i]);
}

return 0;
}


char toupper(char let)
{
int d;
d = (int)let;
let = (char)d-32;
return let;
}

char tolower(char let)
{
int d;
d = (int)let;
let = (char)d+32;
return let;
}

Recommended Answers

All 4 Replies

>it still doesnt work.

You're going to have to provide a little bit more information about your problem than that if you expect any help at all.

Why are you reading charcater one by one, you could read the whole string and then reject if lenght is greater than 9.

Please format your code. We need to be able to follow it in order to be able to help.

And reading this will help you give us the info we need.

This could be used for the input.

int i;
int buffer = 100;
int length;
char names [10][10]; 	/*stores 10 strings of 9 characters long including \o 	*/
char input [buffer]; 	/* holds the input used for validating length 		*/

for(i = 0; i < 10; ++i)
{
	fgets(input, buffer, stdin); 	/* get the users input */
	length = strlen(input); 	/* find out the length of the input */
	if (length > 9)
	{
		return 1; /* exit string too long */
	}
	else
	{
		strcpy(names[i], input); /*copy input to names */
	}
}

for(i = 0; i < 10; ++i)
{
	printf("%s", names[i]);
}

This code asks you to input 9 strings then prints them out, also validates to make sure that they are not longer than 9 characters.

its probably not the most efficient way of doing it but i've tested it and it works.

PS: remember to #include <string.h>

-Matt

EDIT: - Only problem with this code is that is stores the return character, but i cant remember how you get rid of it, too early in morning to think lol

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.