ввода приведены в: the lord of the rings
Ожидаемый результат является: The Lord of the Rings
Но то, что я получаю: The Lord ofof thethe Rings
Как загладить первую букву каждого слова, кроме определенных слов («и», «но», «от»)?
КОД:
#include <cs50.h>
#include <string.h>
#include <stdio.h>
int main(void)
{
string s = GetString();
//to print an upper case char
printf("%c", toupper(s[0]));
for (int i = 1, n = strlen(s); i <n; i++)
{
//not to capitalize and but of
if (isspace(s[i]))
{
if (s[i + 1] == 't' && s[i + 2] == 'h' && s[i + 3] == 'e') {
printf("%c%c%c%c", s[i], s[i + 1], s[i+2], s[i+3]);
} else if (s[i + 1] == 'a' && s[i + 2] == 'n' && s[i + 3] == 'd') {
printf("%c%c%c%c", s[i], s[i + 1], s[i+2], s[i+3]);
} else if (s[i + 1] == 'b' && s[i + 2] == 'u' && s[i + 3] == 't') {
printf("%c%c%c%c", s[i], s[i + 1], s[i+2], s[i+3]);
} else if (s[i + 1] == 'o' && s[i + 2] == 'f') {
printf("%c%c%c", s[i], s[i + 1], s[i+2]);
} else { //capitalize rest of the code:
printf("%c%c", s[i], toupper(s[i + 1]));
i++;
}
} else {
if( (s[i] == 't' && s[i + 1] == 'h' && s[i + 2] == 'e')
|| (s[i] == 'a' && s[i + 1] == 'n' && s[i + 2] == 'd')
|| (s[i] == 'b' && s[i + 1] == 'u' && s[i + 2] == 't')
|| (s[i] == 'o' && s[i + 1] == 'f'))
continue;
else
printf("%c",s[i]);
}
}
}
Пожалуйста, помогите мне решить эту проблему.
время использовать отладчик. –
Этот код будет намного лучше с вариантом 'strcmp()' вместо проверки каждого символа отдельно. – chrisaycock