So right now I'm trying to code a program in C that takes a string and checks for proper punctuation (e.g. ends with '.', '?', or '!'). I am trying to use the strchr function to test and see if the last character in the string is one of the punctuation marks using a if loop within a for loop. However when I run the program it seems to skip the if loop all together.
Here is the program:
#include<stdio.h>
#include<string.h>
int main(void)
{
char string[1000];
int i,length,a,p,q,e;
printf("Please enter a sentence with a valid punctuation.\n\n");
for(i=0;i<1000;i++)
{
fgets(string,2,stdin);
p=strchr(string,'.');
q=strchr(string,'?');
e=strchr(string,'!');
if(string[sizeof(string)-1]=='.'||'?'||'!')
{
printf("\nYay a sentence!");
break;
}
else if((p && q && e)==NULL)
{
printf("You didn't provide any punctuation. Goodbye.");
exit(a);
}
}
printf("You entered the sentence:\n %s",string);
return 0;
}
I have tried it so many different ways such as trying strstr instead or even storing it a different way through gets (which I quickly learned through gcc and some research is not the way to go.)
I am just completely lost on why this isn't working when I enter a sentence without punctuation.
Sorry if this is really simple, I am fairly new to this.
Thanks in advance.