Hello everyone, I'm trying to read in a string of names and reformat them so that the first letter of the first and last name are uppercase and rest are lowercase.For example, it should look something like the following:
void format(string name[]) {
// Used to tell whether should be upper or lower
bool upper = true;
// Loop through name as long as we don't reach a null terminator
for (int i=0; name[i] != '\0'; i++){
// If the upper flag is set, and we're not dealing with a character
// not a space or comma, etc.
if(upper && isalpha(name[i])){
name[i] = toupper(name[i]);
// Reset upper flag
upper = false;
}
// else if we have a comma, then the next name has started...
elseif(name[i] == ','){
upper = true;
}
// Else we must have something that should be a lowercase
else {
name[i] = tolower(name[i]);
}
}
}
You'll notice I removed your n parameter and relating code. That's because I'm assuming that your passing one name at a time If you're not, the you need to pass a 2D array.
EDIT:
Somehow missed that you were using strings. the n parameter along with the .length will work fine, like your original code. The rest should still apply.
Thank you for the help, it still wouldn't compile though :(. The problem is that name[i] is not individual characters but an array of strings. However, the code is set up to only reformat one string. Should I break the string into two parts such as reading the firstname and the lastname?