Need to make a word separator! PLEASE HELP

Hey guys, I need to make a program for HW, I've been on it since yesterday I can't seem to figure it out. I've attempted many different things so not trying to be lazy but I kinda give up lol.
I put a cout statement inside the loop to see what's going on during the iterations and during each iteration it gets set back to the separator char array. I know its because I'm assigning its value again during each iteration but it's the closest I've got, my other codes insert the " " at the element and move things around. Please help, thanks! Sorry if I'm not clear on what I'm saying I'm a noob at this :d Forgot to add, spaces are suppose to come between capital letters.

#include <iostream>
#include <cctype>
#include <cstring>
#include <string>
using namespace std;
void change(string&);
int main()
{
const int SIZE = 50;
char seperator[SIZE];
string test;
cout << "Enter a sentence where all the words are put together\n";
cout << "and first letter in the word is capitalized: " << endl;
cin.getline(seperator, SIZE);


string nString;



test = seperator;
int index = 1;

while(seperator[index] != '\0')
{

if(isupper(seperator[index]))
{

test.assign(seperator);
test.insert(index, " ");


cout << test << endl;
}
index++;
}
cout << test << endl;
}

Last edited on
One way to do it:
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
#include <iostream>
#include <cctype>
#include <cstring>

using namespace std;

int main()
{
  const int SIZE = 50;
  char seperator[SIZE];
  
  cout << "Enter a sentence where all the words are put together\n";
  cout << "and first letter in the word is capitalized: " << endl;
  
  cin.getline(seperator, SIZE);

  int len = strlen(seperator);

  for (int i = 0; i < len; i++)
  {
    if(isupper(seperator[i]))
      cout << ' ';    
    cout << seperator[i];
  }
  cout << "\n\n";
  system("pause");
}
Did you run it? It's not working for me..
Works fine for me. You could be a bit more specific when you say "not working".
Actually, it did I just copied and pasted, however, can you explain why system pause is used cause i didn't even learn about that in the book so I think its probably not the way the book wanted me to do it. Thanks though
You dont need it at all, just more it. On visual studio, there is a problem where the console instantly closes, so you can't see the output etc. So when you're trying stuff and praciting programming people use system pause so they can see the console, that's really all.
Haha, you answered so fast before i even got to post haha, sorry to bother you man, I tried to do the rest on my own, I need to leave just the first letter capitalized, I static casted after using to lower function, but it changed the first letter as well, so i tried setting index to 1 rather than 0 to skip first letter, and it types a blank for the first element
I think I might've got it, I just used to upper after words to change it back
Grr, sorry man it'll work that way but it puts a ' ' infront of the sentence. So when i try to start it from index 1 rather than 0 it omits the first letter
Hey, Im gonna write it here since you can't be PM:ed. Dont currently have much time to take a good look at your program, hopefully someone will. But if you still got problems later and if you need any help with any other programs you can feel free to PM me your skype, some people learn more when it's a bit more person.
Sounds good I appreciate it, i'm new to the forum may be thats why and I definitely will.
Do I understand correctly that you don't want a space before the first letter?
If this is the case just change line 21 to if(isupper(seperator[i]) && i > 0)
Yup, wow how do you guys figure this stuff out lol. I've literally been on this hw since yesterday, I mean usually for a beginner I figure stuff out pretty quick. Like my book said nothing about having to static cast after using toupper/lower function. Where and how did you learn c++ if you don't mind me asking?
Nice, I'm jealous haha. I just started programming, its interesting but discouraging at the same time but I like the challenge. Do you have any tips for me like which steps should I take? I want to get into cyber security.
Topic archived. No new replies allowed.