Я, вероятно, ужасно реализую этот код, но сейчас я делаю CS50 и пытаюсь найти в моей строке символ '. Я искал другие символы, такие как text [i] == '!'
, однако при выполнении text [i] == '''
он работает неправильно. Можно ли заставить его работать таким образом?
Вот мой ужасный код, если вам интересно ... я пытаюсь найти количество букв, слов и предложений ... это работает не так, как символы я не могу определить.
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
int main (void)
{
string text = get_string("Input text: "); //accept text input
int i;
int length = strlen(text);
int count = 0;
int count2 = 0;
int count3 = 0;
int excludeothers = (length - count);
for (i = 0; i < length; i++)
{
if(text[i] == ' ' || text [i] == '!' || text[i] == '?' || text[i] == '.' || text[i] == ',' || text[i] == '"' || text[i] == ':' || text[i] == ';' || text[i] == '-' || text[i] == ''') //check number of letters
{
count++;
}
}
for (i = 0; i <= length; i++)
{
if((text[i] == ' ' || text[i] == '\0') && (text[i-1] != ' ' || text[i-1] != '\0')) //check number of words
{
count2++;
}
}
for (i = 0; i < length; i++)
{
if((text[i] == '.' || text[i] == '?' || text [i] == '!') && (text[i+1] == ' ' || text[i+1] == '\0')) //check number of sentences
{
count3++;
}
}
printf("%i letters\n", excludeothers); //print letters
printf("%i words\n", count2); //print words
printf("%i sentences\n", count3); //print sentences
}