Replace numbers with x unless it is part of a word with letters

Hi everyone,

I am new to all computer programming and am taking a course in C++. I thought I was doing well at the beginning, but since the class is advancing, I am having much more trouble. I have been working on the code for this problem for several days. This is my most recent iteration, which I really think should work, but it obviously does not.

The program must replace all digits with "x" unless the digit is part of a word, like "james007". I have found other references online for this problem, but all suggestions use more advanced libraries than I am up to. The code I am including has all the libraries I can use.

If anyone can help point out why my code isn't working, I would really appreciate it.
Thank you

#include<iostream>
#include<string>
using namespace std;


int main()
{
string statement;

int length, i, space;

int start=0,a;
bool foundLetter=false;
space = statement.find(' ', start);

cout << "Please enter a line of text:\n";
getline(cin, statement);

length = statement.length();

for (i = 0; i < length; i++) {
space = statement.find(' ', start);
for (a = start; a < space; a++) {
if (isalpha(statement[a] == true)) {
foundLetter = true;
}
else if (isalpha(statement[a] == false)) {
foundLetter = false;
}
}
if (foundLetter = false) {
for (a = start; a < space; a++) {
statement[a] = 'x';
}
}
start = space;
foundLetter = false;


}
cout << statement;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <cctype>

int main()
{
    std::string sentence ;
    std::getline( std::cin, sentence ) ;
    std::cout << sentence << '\n' ;

    // replace all digits with "x" unless the digit is part of a word, like "james007".
    // note that this code assumes that words do not start with digits. ie. not "007james"
    bool in_word = false ;
    // http://www.stroustrup.com/C++11FAQ.html#for
    for( char& c : sentence ) // for each char in the sentence
    {
        if( std::isspace(c) ) in_word = false ; // space, not not inside a word
        else if( std::isalpha(c) ) in_word = true ; // alpha, a new word is starting

        if( !in_word && std::isdigit(c) ) c = 'x' ;
    }

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

http://coliru.stacked-crooked.com/a/8edccd97242a8d81
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main()
{
   const string DIGITS = "0123456789";
   string sentence;
   cout << "Enter a sentence:\n";   getline( cin, sentence);

   int p, q = 0;
   while( q != sentence.size() )
   {
      p = sentence.find_first_of    ( DIGITS, q );   if ( p == string::npos ) break;
      q = sentence.find_first_not_of( DIGITS, p );   if ( q == string::npos ) q = sentence.size();
      bool space_before = ( p == 0               || isspace( sentence[p-1] ) );
      bool space_after  = ( q == sentence.size() || isspace( sentence[q  ] ) );
      if ( space_before && space_after ) sentence.replace( p, q - p, string( q - p, 'x' ) );
   }

   cout << sentence << '\n';
}


Enter a sentence:
007James Bond007 and 101 Dalmat10ns sink T1tan1c 2
007James Bond007 and xxx Dalmat10ns sink T1tan1c x
Last edited on
Topic archived. No new replies allowed.