- #1
liukinhei
- 2
- 0
Hello,
I am writing a c program to Prompts the user (altogether 10 times) to enter a word not longer than 9 characters: for this part, it doesn't seems working. it didnt return back to the loop to re-entry a correct word(less 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- i can only print out the word (my input : chester, output CHESTER). it can't show like (my input : Chester , output RETSEHccHESTER) , 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 doesn't work. Thanks in advance for the input.
I am writing a c program to Prompts the user (altogether 10 times) to enter a word not longer than 9 characters: for this part, it doesn't seems working. it didnt return back to the loop to re-entry a correct word(less 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- i can only print out the word (my input : chester, output CHESTER). it can't show like (my input : Chester , output RETSEHccHESTER) , 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 doesn't work. Thanks in advance for the input.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char toupper(char);
char tolower(char);
int main(void)
{
#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[10][19];
while ( j != 10)
{
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';
break;
}
}
}
printf(" you have entered more than 9 characters! \n"); }
for ( i = 0; i < 10; i++ )
{
for ( j = 0; j < 10; j++ )
{
k = (int)(words[i][j]);
if ( k >= 64 && k <= 91 )
{
words[i][j]= tolower(words[i][j] );
}
else if ( k >= 96 && k<= 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[j],words[j]);
strcpy(words[j],words[j+1]);
strcpy(words[j+1],swap[j]);
}
}
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;
}
Last edited: