I am working on a C function which must input a string and remove all the non-letter characters at the beginning only. For example, if the input string was "123 456 My dog has fleas."
then the output string must be: "My dog has fleas."
Here's what I have, which works on the above example:
int isALetter(char x){
// Checks to see is x is an ASCII letter
if( ((int)x>=65 && (int)x<=90) || ((int)x>=97 && (int)x<=122) )
return 0; // TRUE
return 1; // FALSE
}
char* removeNonLettersAtBeginning(char* str){
while( isALetter(str[0]) == 1 && &str[0] != NULL )
str++;
return str;
}
Here's what bugs me... If the string has no letters at all, the code doesn't seem to work. If I submit string " "
(no letters) then I get "XDG_SESSION_ID=3818
". I don't know what that string is, but I'm assuming its "garbage" in the system.
But my removeNonLettersAtBeginning()
function should be returning a "" string, an empty string. I can't figure out what the problem is, but I'm betting it lies here:
while( isALetter(str[0]) == 1 && &str[0] != NULL )
The "&str[0] != NULL"
part of that line is to ensure I don't run off the end of the string; I'm trying to check to see if I've hit the Null character which terminates the string. Anyone see where I'm going wrong?