Ok so I have the easy part of this problem figured out but the other part is confusing me. Here's the problem:

Write a program that reads in a line of text and outputs the line with all the digits in all integer numbers replaced with 'x'.

Example
Input: My name is tony95 and my 5 digit zip is 89115

Output: My name is tony95 and my x digit zip is xxxxx

The problem I have is that ALL the digits are converted to x including 'tony95' becomes 'tonyxx'. I need only the integer numbers to be converted not the combined char/int. Please help! Here's what I have so far:

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


int main()
{
	int i;	
	string st1;

	cout << "Now enter a line of text: ";
	getline(cin, st1);
	cout << endl;

	for (i = 0; i <= st1.length( ); i++)
	{
		if (st1[i] >= '0' && st1[i] <= '9')
		{
			st1.at(i)='x';
		}
	}

	cout << "\nNew string with numbers replaced with 'x':  " << st1 << endl << endl;
		
	return 0;
}

The numbers you are looking for are surrounded by spaces (or in the last case by space and the end of the string). If you hit a space and the next digit is a number, start converting until you hit another space(or the end of the string).

Without replacing the digits with x as you find them, remember the first digit you encounter while you scan the string. Move ahead until you find a non-digit. Replace sub-string from the first digit to the one before the non-digit with a single "X".
There are many ways to do this, but one basic algorithm would be as below.

Number_Start_Pos = -1
Number_End_Pos = -1;
read character while not end of string{
   If ( ( Character is a digit ) AND ( Number_Start_Pos == -1 ) ){ 
       Number_Start_Pos = current_position;
   }
   If ( ( Character is not a digit) AND ( Number_Start_Pos != -1 ) ) {
       Number_End_Pos = current_position - 1 ;
       Replace( Number_Start_Pos to Number_End_Pos with "X" );
       Number_Start_Pos = -1;   
       Number_End_Pos = -1;
   }
}

Ok I figured out how to do it up to the point of if the first char is an integer. The biggest problem is basically when the very first char in the code is a digit, also if the first char after a space is a digit but has a char after that it still converts it which it shouldn't. Example 115d becomes xxxd which it shouldn't. Thanks for the help thus far but I've tried what you guys have told me and still no luck. Also I need the code to stay somewhat basic I'm not that advanced yet with c++. Can you tell me what exactly is wrong with my code thanks.

int i;	
	string st1;

	cout << "Now enter a line of text: ";
	getline(cin, st1);
	cout << endl;

	for (i = 0; i <= st1.length( ); i++)
	{	

		if(isdigit(st1[0]))
		{
			st1.at(i)='x';

			do
			{
				st1.at(i)='x';
				i++;
			}while(!isdigit(st1[i]));
		}
	
		if (st1[i] == ' ')
		{
			i++;
			while(isdigit(st1[i]))	
			{
				st1.at(i)='x';
				i++;
			}
		}
	}

	cout << "\nNew string with numbers replaced with 'x':  " << st1 << endl << endl;
		
	return 0;
}

You are on the right track, but here's some more pseudocode

IF (it is a digit and we have encountered a space already)
   Keep a counter of the number of digits we have passed over

ELSE IF (it is a letter or otherwise)
   Turn off the flag saying we encountered a space
   Go back through the loop   

ELSE IF (it is a space and we have already encountered one)
   Back up the number of digits in the count and overwrite

Since the last character of the string could be a number or letter (or space really, but that should be taken care of), check for this outside of the loop and use the digit counter to go back accordingly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.