This results in undefined behaviour:
This makes you vulnerable to a buffer overflow:
Code:
gets(originalString);
I suggest that you write isalpha for a string, e.g.,
Code:
int isalphaString(const char *str)
{
for (; *str != '\0'; ++str)
{
if (!isalpha(*str))
{
return 0;
}
}
return 1;
}
Now, in the loop where you request for the user to enter the string, read in the input using something safer like fgets(), then use isalphaString() to check if the input is correct. If it is, end the loop, otherwise print the error message and request for user input again.
If you do use fgets(), then note that it places the newline in the string unless the string read is of the maximum length. Consequently, it may be useful to write a function to simplify the process of removing that newline, then replace the fgets() call with it, e.g.,
Code:
char *getLine(char *str, size_t size, FILE *fp)
{
if ((str = fgets(str, size, fp)))
{
char *new_line = strchr(str, '\n');
if (new_line)
{
*new_line = '\0';
}
}
return str;
}