Pointers

Please help me with line 45:

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

using namespace std;
int SpacesRemoval(char* Str);

int main()
{
	char Str[100];
	cout << "Enter a sentence and I will strip out all of the spaces. " << endl;
	cin.getline(Str, 99);
	
	cout << "Your sentence without spaces in it is: " << endl;
	
	cout << "I removed " << SpacesRemoval(Str) << " spaces from the sentence." << endl;

	return 0;
}
int SpacesRemoval(char* Str)
{
	char* removal = Str;
	char* temp = Str;
	int len = strlen(Str);
	int count = 0;
	char *p;
	p = Str;
	
	
	while (*p)
	{
		switch (toupper(*p++))
		{
		case ' ':
			
			count++;
		}
	}
	while (*removal)
	{
		if (*removal != ' ')
			*temp++ = *removal;
		removal++;
	}
	*temp = 0;
	cout << Str << endl; // I am not supposed to use 'cout' in this function but I am confused in how to eliminate it, as the program doesn't work if I just remove it.
	return count;
}
Last edited on
Can anyone give me hint??
Simply copy temp to Str and print Str in main().
http://www.cplusplus.com/reference/cstring/strcpy/
Can anyone give me hint??

It may be pretty complicated because you are not using std::string.
Last edited on
SpacesRemoval is a function which modifies the C-string its parameter points to. Remove the output from SpacesRemoval and simply call the function and save the return value rather than placing the call within the output.

Then, you can use the saved value to output the number of spaces removed as well as the changed string to output the changed contents.

In other words, you just need to shuffle around the order in which you are doing things a little bit.
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
#include <iostream>
#include <cstring>

using namespace std;

void removeCharacterFromString(char* str, int idx, int &size);
int spacesRemoval(char* str, int size);

int main()
{
	char myString[1000];
	cout << "Enter a sentence and I will strip out all of the spaces. " << endl;
	cin.getline(myString, 999);
	
	cout << endl;
	
	cout << "+ I removed " << spacesRemoval(myString, strlen(myString)) << " spaces from the sentence." << endl;

	cout << "+ My final string is : " << myString << endl;

	cin.get();
	return 0;
}

void removeCharacterFromString(char* str, int idx, int &size)
{
	int i;
	for(i = idx; i < size - 1; i++)
	{
		str[i] = str[i+1];
	}
	size--;	str[size] = '\0';
}

int spacesRemoval(char* str, int size)
{
	int count = 0;
	int i;
	for(i = 0; i < size; i++)
	{
		if(str[i] == ' ')
		{
			count++;
			removeCharacterFromString(str, i, size); i--;
		}
	}
	return count;
}


Enter a sentence and I will strip out all of the spaces.
This is my dog and here is my cat "Tony".

+ I removed 9 spaces from the sentence.
+ My final string is : Thisismydogandhereismycat"Tony".

http://cpp.sh/5tql
Topic archived. No new replies allowed.