My program is designed to make the plural of a noun. The error comes from the line "char *pstr = userNoun[lengthStr - 1];". Could someone tell me what my mistake was here?
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
void pluralNoun(char userNoun[27]){
int lengthStr = 0;
const char caseOne[2] = "es";
const char caseTwo[2] = "s";
lengthStr = strlen(userNoun);
char *pstr = userNoun[lengthStr - 1];
if(strncmp(pstr - 1, "ch", 2) == 0){
strcat(userNoun, caseOne);
}
else if(strncmp(pstr - 1, "sh", 2) == 0){
strcat(userNoun, caseOne);
}
else if(*pstr == 's'){
strcat(userNoun, caseOne);
}
else if(*pstr == 'y'){
userNoun[lengthStr - 1] = 'i';
strcat(userNoun, caseOne);
}
else {
strcat(userNoun, caseTwo);
}
printf("The plural of your noun is %s\n", userNoun);
}
int main(void){
char userVar[25];
printf("Enter a noun no more than 25 characters in length in lower case letters:\n");
scanf("%s", userVar);
pluralNoun(userVar);
return 0;
}
char *pstr = &userNoun[lengthStr - 1];userNoun[lengthStr - 1]is achar. If you want a pointer to the address of the char, you should do&(userNoun[lengthStr - 1]), or user pointer arithmetic directly.