capitalization problem

Hello!
I'm using the code below:

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!
just to help people help you =P :


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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]);
        }
    }
}

Nevermind, I figured it out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
How does the isspace() function get the string to check for a space if you give it the index?
Hey! That fixed the original code. I don't know how many times I missed that mistake when I went over this code.
Thanks.

Yeah, the isspace check should be "isspace(list[i].name[letterNum - 1]).
Last edited on
Topic archived. No new replies allowed.