I've recently been tasked with a program with the following details:
3 functions - upperc() ~takes in a lowercase letter and converts it to
uppercase~
lowerc() ~takes in a uppercase letter and converts it to
lowercase~
titleprint() ~prints a string with the first letter of each word
capitalized~
Problem: titleprint() ~Everytime I input a string, the program outputs the
string with the first letter of the first word
'disappeared' and the second letter capitalized
Can someone give me clues on what I'm doing wrong with the code? Help is much appreciated.
I would recommend simplifying some of your program's logic. Checking for space, lowercase, or uppercase is not needed in your titleprint function. This is because you check in lowerc and upperc such that non-alphabet characters will be unaffected.
Simplified version:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void titleprint(char* d) {
int capitalize, i, length;
//char d[300];
//fgets(d, 300, stdin); //this would require removing d as a parameter
gets(d);
length = strlen(d);
capitalize = 1;
for(i = 0; i < length; i++) {
d[i] = (capitalize == 1) ? upperc(d[i]) : lowerc(d[i]);
capitalize = (d[i] == 20) ? 1 : 0;
}
printf("%s", d);
}
I second m4ster r0shi's recommendation to use fgets. Especially if this function is accepting a pointer from who knows where.
Edit: I had the realization that each word needed the first letter capitalized.
Case checking is indeed not needed in titleprint, however I think checking for whitespace
is necessary for the program to work correctly when multiple words are entered as input.