Accessing individual characters in string array?
Aug 11, 2015 at 5:58pm UTC
I have to capitalize the first and last name in my string array, but I'm stuck on how to access the specific value after I input my string value into an array.
Here are my two functions, the first one reads the values into two separate arrays, and the second is supposed to capitalize the first and last names while changing all other characters to lower case.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
void read(string names[],int DVDrent[],int & n)
{
n=0; // # of data calculated
cin >> names[n] >> DVDrent[n]
while (cin)
{
n++;
cin >> names[n] >> DVDrent[n];
}
}
void reformat(string& names)
{
int nlen;
nlen = names.length();
names[0] = toupper(names[0]);
for (int i=1; i<nlen; i++)
{
if (names[i] == ',' )
names[i+1] = toupper(names[i+1]);
names[i+2] = tolower(names[i+2]);
}
}
For reference, the data that I enter is as follows.
"./a.out < data > output"
Data:
smith,EMILY 3
Johnson,Daniel 2
williAMS,HanNAH 0
joneS,Jacob 4
bROwn,MicHAEL 5
DAVIS,ETHAn 2
millER,soPhiA 0
TAYlor,matthew 1
andERSON,aNNa 7
Desired Output:
Smith,Emily 3
Johnson,Daniel 2
William,Hannah 0
.
.
.
Anderson,Anna 7
etc.
Aug 11, 2015 at 6:05pm UTC
Aug 11, 2015 at 7:06pm UTC
Try this if you haven't already done it :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void reformat(string &names)
{
int nlen;
nlen = names.length();
for (int i = 0; i<nlen; i++)
{
names[i] = tolower(names[i]);
if (i == 0)
{
names[i] = toupper(names[i]);
}
if (names[i] == ',' )
{
names[i + 1] = toupper(names[i + 1]);
i++;
}
}
}
Last edited on Aug 11, 2015 at 7:07pm UTC
Topic archived. No new replies allowed.