reformating using array of string.

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:

Sample Input:
wiLL,smiTh
sarah,LeAH
Expected Output:
Will,Smith
Sarah,Leah

I didn't put the complete code in here because I don't want it to be too confusing. But I am using the following function to reformat the words.

I think there's something wrong with the parameter I'm passing..... but I don't know how to fix it.


1
2
3
4
5
6
7
8
9
10
11
 void format(string name[],int n)
{
  //n is the number of names read.
  for (int j=0; j<n; j++)
    {
      name[0] = toupper(name[0]);
      for (int i=1; i<name.length(); i++)
        name[i] = tolower(name[i]);
    }
}


Thank you for any help.
Last edited on
Well, the first thing I see is .enght(), which I would assume should be length(), however, this property doesn't exist with C++ char arrays.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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...
    else if(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.
Last edited on
Last edited on
std::toupper and std::tolower work on individual characters, not strings.

1
2
3
4
5
6
7
8
9
10
 void format(string name[],int n)
{
  //n is the number of names read.
  for (int j=0; j<n; j++)
    {
      name[j][0] = toupper(name[j][0]);
      for (int i=1; i<name.length(); i++)
        name[j][i] = tolower(name[j][i]);
    }
}

Last edited on
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?
Last edited on
No, just do as Cire did. Access each character via name[index][character].
Here is the example from http://www.cplusplus.com/reference/locale/toupper/ slightly modified:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <locale>
using namespace std;

int main ()
{
	locale loc;
	string str="Test String.\n", newstr;
	for (size_t i=0; i<str.length(); ++i)
	{
		char c = str[i];
		
		if (islower(c,loc))
			c=toupper(c,loc);
		else
			c=tolower(c,loc);

		newstr.append(1, c);
	}
	cout << str << newstr;
        return 0;
}

giving the output:

1
2
Test String.
tEST sTRING.
Last edited on
Topic archived. No new replies allowed.