Capitalizing specific elements of array using toupper

How would I capitalize the first and fifth element of "the fox" using toupper.
i cant figure 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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <locale>
using namespace std;
 
int main()
{
    int i = 5, j=51, k=62;
    int data[5] = {10, 20, 30, 40, 50};
    char my_cstring[8] = "the fox";
	std::locale loc;
 
    
    char *pc = my_cstring;
	
	int *p = &i;
	cout<< "dereferenced p= " << *p<<endl;
	
	j=*p;
	k=*p;
	cout << "j= " << j << endl << "k= " << k << endl;

	p= data;
	cout << "p equals " << *p << endl; 

	p=data+2;
	cout << "the 3rd element is " << *p << endl;

	// need to do part e

	while (i==0||i==4)
		toupper(my_cstring[i],loc);
	cout<< my_cstring<<endl;
	
	


 
    cout << "done"<<endl;
    #ifdef WIN32
    system("pause");
    #endif
    return 0;
}
If it was me I would just step through the char[] and correct capitalization as needed.

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
#include <iostream>

using namespace std;

int main(){

	bool flag = false;
	char ary[] = "the fox";
	
	ary[0] = toupper(ary[0]);

	for (size_t i = 0; i < strlen(ary); i++){
		if (flag){
			ary[i] = toupper(ary[i]);
			flag = false;
		}
		if (isspace(ary[i]))
			flag = true;
		cout << ary[i];
	}

	cout << endl;

	return 0;
}
Last edited on
@Mobotus umm..you know what elements they are..

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>

int main()
{
    std::string str = "the fox";
    str[0] = toupper( str[0] );
    str[4] = toupper(str[4] );
    std::cout << str << std::endl;
}


You can use c string instead of std::string if you want.

Anyways in your original code you should get rid of the while on line 30 and make it an if statement then get rid of the , loc not sure what that is also to upper returns an integer and takes 1 integer as param.

Basically here is how the toupper function looks like.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int toupper( int c )
{
    if( c >= 'a' && c <= 'z' )
        return( c - ' ' );
    return( c ); //else return orig value
}

//or

int toupper( int c )
{
    if( c >= 97 && c <= 122 )
        return( c - 32 );
    return( c ); //else return orig value
}


http://www.cplusplus.com/reference/cctype/toupper/?kw=toupper

*typo
Last edited on
@giblit
Yes you can see what elements they are in that example but I was thinking in more general terms for correcting capitalization. Instead of fixing "the fox" now I have a method that works on more than that if I would need it.
Last edited on
Ah yeah that would be more practical to be honest I didn't even realize he was trying to make it camel case. I just though he randomly picked those two elements.

OP wrote:
How would I capitalize the first and fifth element of "the fox" using toupper.
Topic archived. No new replies allowed.