Editing element of a parallel array

I am working on a program which (in part) reads input through a function get_data(string[], int[], int&) then outputs the data in the correct format. The data is input into a parallel stream.

Expected input (notice the lack of space between first and last names)
waSHiNGtOn,geOrGe, 8
liNCOln,Abraham, 10

Expected Output:
Washington,George, 8
Lincoln,Abraham, 10

I have attempted isupper, tolower, commands etc. but they don't seem to work on the array.
Any suggestions would be greatly appreciated.
teed999 wrote:
I have attempted isupper, tolower, commands etc. but they don't seem to work on the array.
These functions act on a single character only.

teed999 wrote:
parallel array
This is a bad idea - consider using a struct, pair, or tuple instead.
Last edited on
The parallel array is required (per assignment) and specifically prohibits struct, pair, tuple.
Well then the first part of LB's answer is what you want to concentrate on. You need to iterate through each element of the array and apply whichever isupper or tolower command as required.
This has nothing to do with the parallel arrays...

You've got to 'titlecase' a string. Hint:

1
2
3
4
5
void to_titlecase( char* s )
{
  // make each character in s[] lowercase, 
  // except for the first, which should be uppercase
}

My perhaps naive solution would be to test each character but first you must extract them.
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string lastName[2];
    string firstName[2];
    int item[2];

    lastName[0] = "waSHiNGtOn";
    firstName[0] = "geOrGe";
    item[0] = 8;

    lastName[1] = "liNCOln";
    firstName[1] = "AbraHam";
    item[1] = 10;

    for (unsigned int j=0; j < 2; j++)  // for each item in lastString array
    {
        for (unsigned int i=0; i < lastName[1].length(); i++)  // for each character in each item
        {
            cout << lastName[j].at(i) << " ";
        }
        cout << endl;
    }
    return 0;
}

Last edited on
Thanks for all the suggestion thus far.
Here is what I have for the function thus far. Note it fulfills all requirements except capitalizing the firs name(which occurs directly after the comma).
Any suggestions on capitalizing the first letter after the comma?

void alter_name( string nm[], int n)
{
string wrd;

for(int i=0; i<n; i++)
{
wrd = nm[i];
wrd[0] = toupper(wrd[0]);
for(int j=1; j< wrd.length(); j++)
{ wrd[j] = tolower(wrd[j]);}
nm[i] = wrd;
}
}
Last edited on
Just test if the current character was preceded by a comma ',' then uppercase else lowercase

Last edited on
Oh, I get it.
"waSHiNGtOn,geOrGe, 8" is all one string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void to_titlecase( char* s )
{
  bool last_was_a_letter = false;
  while (*s)
  {
    bool is_current_a_letter = isalpha( *s );
    if (last_was_a_letter)
      *s = ...;
    else
      *s = ...;
    last_was_a_letter = ...;
    s++;
  }
}

Hope this helps.
Topic archived. No new replies allowed.