Every other word

Pages: 12
Im making a program to output a sentence where every other word is an asterik
like this: The dog is brown: would be The *** is ****. However my current code doesnt even output anything, it just receives the input, and i need help with the asterik part
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
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
	char str [100];
	int wordcount=0;

	cout << "Enter sentence: ";
	gets (str);

	for (int i=0; i <wordcount; i++)
	{
		while (str[i] < 0)
		{
			cout << "Redacted:" << str;
			if (str[i]==' ')
				cout  << '*';
			wordcount++;
		}
			
	}

	return 0;
}


Your loop

for (int i=0; i <wordcount; i++)

is never executed because the initial value of wordcount is equal to 0. So the condition i <wordcount is always false
What should it be less than then? i tried using 100, but then it just repeats 100 times and thats no good
How about:
Ask the user to enter a sentence
get the whole line to a string
construct an istringstream from the string
while istringstream can extract a word
    print the word
    ignore everything until a space
i just want it to stop repeating infinetly
Think. You increment wordcount in the for loop. You are looping for wordcount times. You have a logic error, my friend.
Changed my code a bit, thought it would be better, it makes more sense to me but it still repeats.

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

using namespace std;

int main()
{
	char str [100];
	
	int i=-1;

	cout << "Enter sentence: ";
	gets (str);

	
		while (str[i] != 0)
		{
			cout << "Redacted:";

			while (str[i] != ' ')
				cout << str;
			
			if (str[i] ==' ')
				cout  << '*';
			
				
		}
			
	

	return 0;
}
i starts at -1, and then never changes, so not only are you accessing memory out of bounds, you are also accessing the same memory over and over, and setting it to different memory in such a way that the condition of it being equal to 0 is never true.
okay, say i remove the i variable entirely, and it is just while (str !=0) shouldn't it read through the string until it sees a 0, which would be at the end of whatever you entered? when i enter something in this case, it just continuously repeats
str is a stack-allocated array and will never be equal to 0. Thus, infinite loop.

Try this on for size:
1
2
3
4
5
6
7
8
9
10
11
12
for(unsigned i = 0; str[i] != 0; ++i)
{
    boolean makestars = false;
    if(str[i] == ' ')
    {
        //I'll let you write the correct line of code here involving makestars
    }
    else if(makestars)
    {
        //I'll let you write the correct line of code here involving str[i]
    }
}
Maybe you want something like 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
34
35
36
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

    int main()
    {
		int i = 0;
		string strArray[20];
		string str;
	        string word;
	
		cout << "Enter sentence: ";
		getline(cin, str);
		stringstream stream(str);

		 while( getline(stream, word, ' ') && i < 10 ) {
					strArray[i] = word;
					i++;
				}

			for (int i = 1; i < 10; i+=2) {
				string txtReplace ("");
					for (int y = 0; y < strArray[i].length(); y++)
						txtReplace += "*"; 

				strArray[i].replace(strArray[i].begin(), strArray[i].end(), txtReplace);
	  
				}

				for (int i = 0; i < 10; i++)
 				 cout << strArray[i] << " ";

			return 0;
    }
Last edited on
That's obscenely over complicated for such a simple task. Keep things simple.
Ok now why wont this stop repeating infinetly

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

using namespace std;

int main()
{
	char str [100];
	int i=0, length;
	
	length= strlen (str);

	cout << "Enter sentence: ";
	gets (str);

	cout << "Redacted:";
	while ( str[i] < length )
	{
			

			while (str[i] != ' ')
				cout << str;
			
			if (str[i] ==' ')
				cout  << '*';
			
			i++;
	}
			
	

	return 0;
}
Let's make an exercise. Put a comment in every line that tells what do you think that line is doing.
Then I will show you the correct syntax to accomplish that.
while ( str[i] < length ) //while str[i] is less than the length of the actual string of characters
{


while (str[i] != ' ')
cout << str; //should print the sentence until it sees a null character

if (str[i] ==' ') // if it sees a null character then print a star (although i want it to print the number of stars between the first null character until it sees another null)
cout << '*';

i++;
}
You fail on the first line. Tip: str[i]
' ' is a space
'\0' is the null character

while str[i] is less than the length of the actual string of characters
that makes no sense.
Keep in mind that str[i] is a character. So if your string is "hello_world" you will be comparing 'h' against 11.

1
2
while (str[i] != ' ')
  cout << str; //should print the sentence until it sees a null character 
¿don't you realize that the condition never change?
If you want to show the string till a whitespace you may do
1
2
3
4
while(str[K] != ' '){
  cout << str[K]; //character by character
  ++K; //changing the index
}
alright i got my program working but where i have if (i > length) is there any other way you guys could suggest to cut out of that loop to get it to output the proper text and stars? i mean it works but my specs say i should user a different method.

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <cstring>
#include <fstream>

using namespace std;

int main()
{
	char str [100];
	int i=0, length;
	
	ofstream outfile ("redacted.txt");

	cout << "Enter sentence: ";
	gets (str);

	length= strlen (str);

	cout << "Redacted: ";
	while ( i < length )
	{
			

			if (str[i] != ' ')
			{
				cout << str[i];
				outfile << str[i];
				i++;
			}

			else 
			{
				cout << ' ';
				outfile << ' ';
				i++;
				
				
				while (str[i] !=' ')
				{

					cout  <<'*';
					outfile << '*';
					i++;
					
					if (i > length)
						break;
					
				}
			
				if (i > length)
						break;

				cout << ' ';
				i++;

				if (str[i] != ' ')
				{
					cout << str[i];
					outfile << str[i];
					i++;
				}
				
				

				

			}
		}
			
	cout << endl;
	outfile << endl;

	return 0;
}
Your specs? What do you mean? You think it will take ages to loop?
At line 45, is there another way to break out of that loop so it prints the correct amount of stars?
Pages: 12