void nameFormat(info list[])
{
int i, first, letterNum;
//change all names to lower case
for (i = 0; i < MAX_COUNTRIES; i++)
{
for (char *iter = list[i].name; *iter != '\0'; iter++)
*iter = tolower(*iter);
}
//capitalize the first letter of each word
for (i = 0; i < MAX_COUNTRIES; i++)
{
//capitalize the first letter of each string
list[i].name[0] = toupper(list[i].name[0]);
//capitalize the first letters of all other words
for (letterNum = 1; letterNum < strlen(list[i].name); letterNum++)
{
//if the character is a letter and the previous is a space, then
//capitalize the letter
if ( (isalpha(list[i].name[letterNum])) && (isspace(letterNum - 01)) )
list[i].name[letterNum] = toupper(list[i].name[letterNum]);
}
}
}
I'm attempting to capitalize the first letters of each word from an input file. The first letter capitalization works correctly, but nothing else is capitalized until the 11th character. Then I get the next five letters capitalized and then lower case following.
Does anyone have any idea what might be causing this unexpected output?
Thanks in advance!
void nameFormat(info list[])
{
int i, first, letterNum;
//change all names to lower case
for (i = 0; i < MAX_COUNTRIES; i++)
{
for (char *iter = list[i].name; *iter != '\0'; iter++)
*iter = tolower(*iter);
}
//capitalize the first letter of each word
for (i = 0; i < MAX_COUNTRIES; i++)
{
//capitalize the first letter of each string
list[i].name[0] = toupper(list[i].name[0]);
//capitalize the first letters of all other words
for (letterNum = 1; letterNum < strlen(list[i].name); letterNum++)
{
//if the character is a letter and the previous is a space, then
//capitalize the letter
if ( (isalpha(list[i].name[letterNum])) && (isspace(letterNum - 01)) )
list[i].name[letterNum] = toupper(list[i].name[letterNum]);
}
}
}
void nameFormat(info list[])
{
int i, letterNum;
char prev;
//change all names to upper case
for (i = 0; i < MAX_COUNTRIES; i++)
{
for (char *iter = list[i].name; *iter != '\0'; iter++)
*iter = toupper(*iter);
}
//lowercase all letters without a space before them
for (i = 0; i < MAX_COUNTRIES; i++)
{
for (char *iter = list[i].name, prev = *iter; *iter != '\0'; prev = *iter, iter++)
if (prev != ' ')
*iter = tolower(*iter);
//re-capitalize the first letter of each string
list[i].name[0] = toupper(list[i].name[0]);
}
}
Although if anyone can tell me what was wrong with the first set of code I would still appreciate it.
void nameFormat(info list[])
{
int i, first, letterNum;
//change all names to lower case
for (i = 0; i < MAX_COUNTRIES; i++)
{
for (char *iter = list[i].name; *iter != '\0'; iter++)
*iter = tolower(*iter);
}
//capitalize the first letter of each word
for (i = 0; i < MAX_COUNTRIES; i++)
{
//capitalize the first letter of each string
list[i].name[0] = toupper(list[i].name[0]);
//capitalize the first letters of all other words
for (letterNum = 1; letterNum < strlen(list[i].name); letterNum++)
{
//if the character is a letter and the previous is a space, then
//capitalize the letter
if ( (isalpha(list[i].name[letterNum])) && (isspace(letterNum - 01)) )
list[i].name[letterNum] = toupper(list[i].name[letterNum]);
}
}
}
It kept giving me:
AfghanistaN
Bosnia herCEGOVina
Dominican REPUBlic
Egypt
Germany
Madagascar
United araB EMArites
United staTES
Zimbabwe