I want the user to type a string that contains only 'P' and 'K' characters. So that's my string, declared inside the main function,
char string[30];
and that's a function that returns 1 if the string completes that criteria or 0 if it does not:
int isStringValid(char **string){
int i=0;
while(*(string+i)!='\0'){
if(*(string+i)!='P' || *(string+i)!='K'){
return 0;
}
++i;
}
return 1;
}
I am getting the string from the user with scan f, but the isStringValid function does not seem to work properly. It returns only false whatever string I type!
int main(){
char string[30];
scanf("%s", string);
if(isStringValid(&string)){
printf("Job Done!\n");
}else{
printf("Not recognised!\n");
}
}
Any ideas why its not working?
char *stringrather thanchar **string.&&there, not||(because'P' != 'K').scanf()won't store newline characters.int isStringValid(char * string) { return strlen(string) == strspn(string, "PK"); }return !string[strspn(string, "PK")] ;