mercredi 27 septembre 2017

Checking for puncuation

So, the goal is to check to see if the C style string ends with a period or exclamation mark. However, for some reason, i keep getting false.

bool isItSentence(const char* s)
{
    int x = strlen(s);


    for (int c = 0; s[c] != '\0'; c++)
    {
        if (!isupper(s[0])) return false;
        if (isupper(s[c]) && c > 0) return false;
        if (s[x-1] != '.') return false;
        if (s[x-1] != '!') return false;
    }
    return true;
}
int main()
{
    std::string str = "Smelly.";
    reverse(str.c_str());

    std::cout << isItSentence(str.c_str()) << std::endl;
    std::cout << strlen(str.c_str()) << std::endl;
    system("pause");

Heres what I have so far. But when I add the last if statement to handle exclamation marks, it returns zero. Any suggestions?

Aucun commentaire:

Enregistrer un commentaire