So I want to create a code that capitalizes the first letter of every word in a string array, then outputs the string in reverse order. I couldn't print the array in reverse, but with that aside, this is what I came up with:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
char string[100];
int i, j;
char newString[100];
printf("\nEnter string: ");
gets(string);
for (i=0; i <strlen(string); i++){
if (string[i] == ' ' && isalnum(string[i+1])==1){ //if the character is preceded by a space
newString[i] = toupper(string[i+1]); //copy the uppercase of the character to newString
}
if (isalpha(string[0]) == 1){ //capitalize the first character in the string if it is a letter
newString[0] = toupper(string[0]); //copy character to newString
}else{
newString[i] = string[i];
}
}
printf("%s", newString); //preferably, the newString should be printed in reverse order, but I can't seem to do it.
}
If:
Input: curran lennart
Supposed output of this code: Curran Lennart
(What I want: narruC tranneL)
As it is, all I'm getting is an output of:
curran lennarta
An input of 'kate daniels' returns 'kate daniels'. If the input is:
julie olsen
The output is:
julie olsenw
Please, help. :(