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.
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)
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?
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
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.
#include <iostream>
#include <cctype>
#include <cstring>
usingnamespace std;
constint NAME = 21;
constint 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