Replace spaces with '*'

I read a line from a file as a string, and I want to replace the spaces with '*'. I'm doing this because I think the functions in the find family, string::find, might have issues with recognizing spaces. It knows a space is a position in the string, but it doesn't recognize it as a character. So if I compare it against a character, I get in accurate results.

....
while (getline(inputFile, inputString))
{
replacementVariable = inputString.find(' ');
inputString.insert(replacementVariable, '.');
....

what am I doing wrong?
cpp300XPBoost wrote:
I'm doing this because I think the functions in the find family, string::find, might have issues with recognizing spaces. It knows a space is a position in the string, but it doesn't recognize it as a character. So if I compare it against a character, I get in accurate results.
This is not true at all. Do you have an example program where spaces cause an issue but other characters don't?
Last edited on
I feel like this might be how to do it in c# not c++
My code looks for +------+ of any length and counts it. Currently these are being counted and i think it's because of the spaces between the '+'

When debugging, an example of the input string it was messing up is:

+---] [---+ +---] [---+ +---] [---+

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
while (getline(inputFile, inputString))
		{
			replacementVariable = inputString.find(' ');
			inputString.insert(replacementVariable, '.');
			//cout << inputString << '\n';
			
			if (inputString.length() > 3)
			{
				string tempString;
				string tempString1;
				vector<int> x = { 0 };
				int i = 0;

				int found = inputString.find_first_of("+");
								
				while (found != std::string::npos)
				{
					
					if (i == 0)
					{
						x[0] = found;
					}
					else
					{
						x.push_back(found);
					}


					found = inputString.find_first_of("+", found + 1);
	
					i++;
				}
				int DummyVariable1 = 0;
				int DummyVariable = 0;
				if (x.size() > 1)
				{
					for (i = 0; i < (x.size()-1); i++)
					{
						//DummyVariable1 = x[i] + 1;
					    DummyVariable = inputString.find_first_not_of("-", x[i]+1);
						//cout << "Find first not of value: " << DummyVariable << '\n';
						if (DummyVariable = x[i + 1] && DummyVariable != string::npos && DummyVariable - (x[i] + 1) > 1)
						{
							complexFound++;
							cout << inputString << '\n';
						}				}
Last edited on
On line 6, are you sure you don't mean i+1 instead of x[i]+1? And similarly for line 8.
Last edited on
Well I understood string::find_first_not_of description as saying that the second input in the () was the position the function starts to do it thing. It compares '-' to whatever is in and after the position you specify. I tried both and stepped through it. x[i] + 1 has been working.
Sorry I updated the code because it wasn't clear what was in x.

It's a vector holding every position it found a '+'.
I can't tell from your code, which part behaves oddly with spaces?
1
2
			replacementVariable = inputString.find(' ');
			inputString.insert(replacementVariable, '.');


This does not replace a space. This just inserts a period before a space.

string::find_first_of is an odd choice for finding a single character.

Your code seems overly complicated. DummyVariable, DummyVariable1... really?

Are you trying to achieve something like the following?

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
// http://ideone.com/DJMwuH
#include <iomanip>
#include <iostream>
#include <string>

unsigned count_occurrences(const std::string& source, char terminal, const std::string& allowed)
{
    unsigned count = 0;
    std::size_t offset = 0;
    while ((offset = source.find(terminal, offset)) != std::string::npos)
    {
        std::size_t next_off = source.find_first_not_of(allowed, offset+1);
        if (next_off != std::string::npos && source[next_off] == terminal)
            ++count;

        offset = next_off; 
    }

    return count;
}

std::string quoted(const std::string& s)
{
    return '"' + s + '"';
}

int main()
{
    std::string test_cases [] = 
    { 
        "+---", 
        "---+ +---", 
        "+--- ---+", 
        "---+", 
        "+--+--+", 
        "+-+", 
        "+--- +", 
        "+ -+" 
    };

    std::cout << std::left;
    for (auto& test : test_cases)
        std::cout << std::setw(15) << quoted(test) << count_occurrences(test, '+', "-") << '\n';
}


string::find_first_of is an odd choice for finding a single character.

Your code seems overly complicated. DummyVariable, DummyVariable1... really?


Yeah, those were for debugging. I was having trouble figuring out when and if I should use x[i] + 1 or x[i+1].

Are you trying to achieve something like the following?


I was trying to do something I thought was simple at first. Count what's called a complex rung in old school ladder logic. I extract code from old school DOS machines and a task that takes forever is counting these rungs. Especially when you have thousands and thousands of networks. So far the counter code gets me really close, but there are exceptions. Right now I'm trying to get ride of those exceptions. But who knows if that's possible.

This is a sample string:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
Network #  457:        Size:   109 bytes


 dM073A    dM073B
---]/[-------]/[---+
                   |
 dM073A    dM073B  |                                                   mM073TB
---] [-------] [---+-----------------------------------------------------(   )

Network #  458:        Size:   158 bytes

 +---------------------------------------------------------------------------+
-|IF fGNBROP==1|fPHBROP==1 THEN vLODREJ=4095;ELSE vLODREJ=819;ENDIF;         |
 +---------------------------------------------------------------------------+
*/


The end result should Count = 1, because I only have one complex rung.
Last edited on
Just thought I'd let everyone here know. I found the "error" last week. It was the

if (DummyVariable = x[i + 1] && DummyVariable != string::npos && DummyVariable - (x[i] + 1) > 1)

there should have been "==" instead of just "=". So the counter now works just fine. I added a few capabilities. I'm now starting on a GUI. It seems like i'll be going with QT for C++ since it's free and there are plenty of tutorials on youtube for it.
Topic archived. No new replies allowed.