C-String Sentences

So I have this assignment for a class where I am supposed to modify a C-String sentence, yes the one from the 80s, and then return that to the user. I am having some trouble with getting it to work properly and I need help making it run. The sentence should start with an uppercase letter and then the rest should be lowercase. It should also delete any extra space that isn't needed- (i.e. "Hello World" becomes "Hello world.". I also need to add a period to the very end of the sentence. Right now I can't get the extra spaces to be removed, here is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  int main()
{
	char userString[101];
	
		
		cout << "Please enter a sentence to be edited: ";
		cin.getline(userString, 100);
		for (int i = 0; i < 102; i++)
		{
			userString[i] = tolower(userString[i]);
			if ((userString[i] == ' '))
			{
				if (userString[i+1] == ' ') 
				{
					//I NEED HELP HERE
				}
			}
		}
			
			userString[0] = toupper(userString[0]);
			cout << userString << endl;
		
Line 8 is incorrect. Valid indices for an array of size 101 are 0 through 100, and you also make no effort to stop the loop when the end of your C-string is found.

You should use two indexes into userString. One to keep track of where you are writing to it and one to keep track of where you are reading from it. If you're skipping space, obviously those two places are going to be different. Also, you must address the issue of extra spaces before you write it back to the string, so line 10 should be after (and dependent on) the conditions you are attempting to address on lines 11/13.
So I need to change my for loop to go to only 101? Then I need to move line 10 to after removing the extra spaces? I just thought that if I had made everything lowercase from the get go, it wouldn't have mattered. I will do that. However, can you give me a little more clarification about the two indexes? Maybe an example? Thank you!
What about this?
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
#include <iostream>
using namespace std;
int main()
{
	char userString[101];
	int space=0;

		cout << "Please enter a sentence to be edited: ";
		cin.getline(userString, 100);
		for (int i = 0; i < 101; i++)
		{
			userString[i] = tolower(userString[i]);

			if ((userString[i] == ' '))
			{
				for(int j=i;userString[j+1]==' ';j++)
                {
                    space++;
                }

                if(space > 1)
                {
                    for(int j=i+1;j<100;j++)
                    {
                        userString[j] = userString[j+space];
                    }
                }
			}
		}

			userString[0] = toupper(userString[0]);
			cout << userString << endl;
}
However, can you give me a little more clarification about the two indexes? Maybe an example? Thank you!


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

int main()
{
    char text[100] = "Mary--had-a-little----lamb";

    // transform to: MaRy HaD a LiTtLe LaMb!

    unsigned in = 0;
    unsigned out = 0;
    unsigned non_spaces = 0;


    while (text[in]) {
        if (text[in] != '-' || (text[in] == '-' && text[in + 1] != '-')) {
            if (text[in] == '-') text[out] = ' ';
            else {
                if (non_spaces % 2 == 0) text[out] = std::toupper(text[in]);
                else text[out] = std::tolower(text[in]);

                ++non_spaces;
            }

            ++out;
        }

        ++in;
    }

    // terminate the string
    text[out++] = '!';
    text[out] = '\0';

    std::cout << text << '\n';
}


Donald Trump wrote:

I wish admin would clean up all your troll accounts again, young delinquent.
Topic archived. No new replies allowed.