Read numeric and change to alpha

I need a program that checks for numeric in a sentence and change it to alpha given by user. If it find alphanumeric it must skip

show what you've got so far.
#include <iostream>
#include <string>
#include <cctype>

int main()
{
using namespace std;

string one_line, sentence="";
cout <<"Enter a sentence."<<endl;
do
{
getline(cin, one_line);
if (sentence.length()==0)
sentence=one_line;
else
sentence=sentence +" "+ one_line;
}while (one_line.find(".")==string::npos);
cout <<"Your sentence corrected for capitalization is"<<endl;
cout <<sentence <<endl;
char current;
bool found_whitespace=false;
for (int i=0; i <sentence.length();i++)
{
current=sentence[i];
if (i==0)
current=toupper(current);
else
current=tolower(current);
if(isspace(current))
{
if (found_whitespace==false)
{
cout << current;
found_whitespace=true;
}
else
{
//do nothing because we have repeat whitespace
}
}
else //not whitespace
{
cout <<current;
found_whitespace=false;
}
}
cout <<endl;
return 0;
}
how has this code got anything to do with what you want to write?
This program should also look for numeric and change it to "X"

#include <iostream>
#include <string>
#include <cctype>

int main()
{
using namespace std;

string one_line, sentence="";
cout <<"Enter a sentence."<<endl;
do
{
getline(cin, one_line);
if (sentence.length()==0)
sentence=one_line;
else
sentence=sentence +" "+ one_line;
}while (one_line.find(".")==string::npos);
cout <<"Your sentence corrected for capitalization is"<<endl;
cout <<sentence <<endl;
char current;
bool found_whitespace=false;
for (int i=0; i <sentence.length();i++)
{
current=sentence[i];
if (i==0)
current=toupper(current);
else
current=tolower(current);
if(isspace(current))
{
if (found_whitespace==false)
{
cout << current;
found_whitespace=true;
}
else
{
//do nothing because we have repeat whitespace
}
}
else //not whitespace
{
cout <<current;
found_whitespace=false;
}
}
cout <<endl;
return 0;
}
Where did you find the above code?

It doesn't seem to look for numeric characters.

You enter a sentence which you must end with a period (full stop) "."

It makes the first character an upper case and converts all other character to lower case.

It checks for a " " character for some reason...

I assume you want to modify it to do something else?

So the code is an example of how to input a sentence and gives an example of how a sentence can be processed.
You need to insert code to process it the way you want.

Here is a simple example in which the process is to convert any lower case characters to upper case.

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
#include <iostream>
#include <string>
//#include <cctype>
using namespace std;

int main()
{
    string one_line, sentence="";
    cout <<"Enter a sentence and end it with a full stop"<<endl;

    // get a sentence ending with a period (full stop).
    do
    {
        getline(cin, one_line);
        if (sentence.length()==0)
            sentence=one_line;
        else
            sentence=sentence +" "+ one_line;
    }while (one_line.find(".") == string::npos);


    // convert any lowercase characters to uppercase characters
    char current;
    for (int i=0; i <sentence.length();i++)
    {
        sentence[i]= toupper(sentence[i]);
    }

    cout << sentence;   // print the result
    cout <<endl;
    return 0;
}

Last edited on
yesy I understand, but in the same program/sentence should the user enter numeric value then it must replace the numeric to an "X"
Just say in words what you want and convert it to C++ code using your knowledge of C++ and library functions like isDigit().

for each letter in the sentence,
if letter is a digit then
make it an "x"

Yes but it should not change eg alphanumeric. Peter88 should remain Peter88 and not PeterXX
Then you need to extract the words from the sentence and determine if they start with something other than a digit. One way might be to look for a space character after which you can check if the next character is a digit or not. Is that what the sample code you gave does?
Your first post did not make it clear that you were talking about more than just a character type.
Figure out how you would do it with pen and paper and convert those steps into C++ code.
Remember a string is just a list of characters and the variable i in the above examples points to the position in that list.

And what about 88Peter?



Last edited on
Topic archived. No new replies allowed.