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;
#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#forfor( char& c : sentence ) // for each char in the sentence
{
if( std::isspace(c) ) in_word = false ; // space, not not inside a word
elseif( std::isalpha(c) ) in_word = true ; // alpha, a new word is starting
if( !in_word && std::isdigit(c) ) c = 'x' ;
}
std::cout << sentence << '\n' ;
}