How to capitalize first letters of a new word in a string

Hello C++ community!

I'm looking for help with some code that I've been working on. I'm at my breaking point it seems. I've tried everything and I can't seem to understand this concept. First let me clarify, I'm not necessarily looking for the answer (code) to properly get this right. I'm looking for someone who might break down the process of how I can rememberer to do this next time. If I can further clarify, I'm looking for someone to explain what code would work to capitalize a each first letter of a new word in a string (in the most beginner way) and how (what is it doing/what does the code mean)? I know that must sound like a weird question, but I'm lost and I'm someone who likes to fully understand things.
The code I posted below is the teacher's solution to the problem I'm having however. It's still not working. I am not sure what I have to do in order to get each letter of a new word capitalized and ultimately that's what I'm looking for help with. Anybody who can help me? Thank you so much! Before anyone recommends read, read, and read some more, I have and I'm diving into every youtube video I can find. I feel lost and this is such an important topic to understand.

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57

// teacher's recommendation
for(int i = 0; i < num; ++i;)
{
  for(int j = i +1; j < num; ++j;)
}


// my code

  4 #include <iostream>
  5 #include <cctype>
  6 #include <cstring>
  7 
  8 using namespace std;
  9 
 10 const int  NAME = 21;
 11 const int  MAJOR = 45;
 12 
 13 
 14 int main()
 15 {
 16 // variables for name (person) and major (declare_m)
 17     char person [NAME];
 18     char declare_m [MAJOR];
 19 
 20 // get user name
 21     cout << "Enter name: " << endl;
 22     cin.get(person, NAME, '\n');
 23     cin.ignore(100, '\n');
 24     person[0] = toupper(person[0]);
 25 // capitalize every first letter after a space
 26 
 27     int len1 = strlen(person);
 28     for (int i = 0; i < len1; ++i)
 29         {
 30            int j;
 31            for (j=i+1; person[j] == ' ' && j < len1; ++j)
 32                person[j] = toupper(person[j]);
 33         }
 34      cout << "Hello " << person << endl;
 35      cout << "Length is " << len1 << endl;
 36 
 37 // gets user major
 38     cout << "What's your major? " << endl;
 39     cin.get (declare_m, MAJOR,'\n');
 40     declare_m[0] = toupper(declare_m[0]);
 41     cin.ignore(100,'\n');
 42     cout << "Your major is " << declare_m << endl;
 43 
 44 
 45 
 46 
 47 
 48      return 0;
 49 }
~       



Thank you!!
Last edited on
The teacher's recommendation is useless, and for(int j = i +1; j < num; ++j;) doesn't need a semi-colon at the end for(int j = i +1; j < num; ++j). The teacher's recommendation basicly is do nothing.
On line 31 of your code, line 38 of the whole thing, you don't need a semi-colon after the for loop for (j=i+1; person[j] == ' ' && j < len1; ++j); should be for (j=i+1; person[j] == ' ' && j < len1; ++j)
Thank you for pointing that out!

Can you explain what this code says? I'm confused with there being a for loop inside of another for loop. How in the world do I identify what's going on here?
I took out the semi colon and it's still not working. When I try to print my name it's still only capitalizing the first word using this code.

Could someone please help me understand why it isn't iterating through each space and then capitalizing?
change line 31 to
1
2
for (j=i+1; !(person[j] == ' ') && j < len1; ++j);
++j;



EDIT:
this line of code for (j=i+1; !(person[j] == ' ') && j < len1; ++j); just sets j equal to the next space in the character array since that's all we want there is no need for the loop to have a body so the ; at the end is what we want.

we actually need the next character after the space so we increment j

we should probably check to make sure j is still within bounds but that's up to you
Last edited on
@SVcpp

You don't really need a double for loop to accomplish the task of capitalizing the first letter in each word. You just check if what is being read IS a space, meaning it's the end of a word. Then capitalize the next character.

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
44
45
46
#include <iostream>
#include <cctype>
#include <cstring>

using namespace std;

const int  NAME = 21;
const int  MAJOR = 45;


int main()
{
 // variables for name (person) and major (declare_m)
 char person [NAME];
 char declare_m [MAJOR];

 // get user name
 cout << "Enter name: " << endl;
 cin.get(person, NAME, '\n');
 cin.ignore(100, '\n');
 person[0] = toupper(person[0]);
 // capitalize every first letter after a space

 int len1 = strlen(person);
 for (int i = 1; i < len1; ++i) // start in 2nd place
 {
	 if(person[i] == ' ') // If a space
      person[i+1] = toupper(person[i+1]); // Capitalize next character in line
 }
 cout << "Hello " << person << endl;
 cout << "Length is " << len1 << endl;

 // gets user major
 cout << "What's your major? " << endl;
 cin.get (declare_m, MAJOR,'\n');
 declare_m[0] = toupper(declare_m[0]);
len1 = strlen(declare_m);
 for (int i = 1; i < len1; ++i) // And do the same for declare_m[]
 {
	 if(declare_m[i] == ' ')
      declare_m[i+1] = toupper(declare_m[i+1]);
 }
 cin.ignore(100,'\n');
 cout << "Your major is " << declare_m << endl;
 return 0;
}
OH! Thank you soo much everyone! I can't thank you enough.

Could you explain what you're doing in line 28? Why does that capitalize? As in, what's the process. I'm just trying to visualize what's happening on the program's end. Sorry I'm a super newb!
It iterates over a string say for example we have "Hello, world!" it will find the space at index 6 so we are assuming the next character is the next word so make index 7 uppercase. T

Thank you giblit
Topic archived. No new replies allowed.