English 中文(简体)
• 如何阅读方位和方位数,从方位到方位
原标题:How to Read charcters and digits from string in c++
  • 时间:2011-11-18 07:38:25
  •  标签:
  • c++
  • string

I m试图读一个显示数字和特性的插图。

Additional Info:
1. the program is showing 10 (ten) as 1 and 0 i.e two separate digits
2. It is also counting space as a character, which it should skip.
3. If a user input 10 20 + it should display:
digit is 10
digit is 20
other Character is +


Here is what I ve tried
#include <iostream>
#include <string>
using namespace std;

int main() {
    string s("10 20 +");
    const char *p = s.c_str();
    while (*p !=   )
    {
        if(isdigit(*p))
        {
            cout << "digit is: "<< *p++ << endl;
        }
        else
        {
            cout<<"other charcters are:"<<*p++<<endl;
        }

    }

    system("pause");
}

www.un.org/Depts/DGACM/index_spanish.htm Edit 现在改为:

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

int main() {
               string x;
string s("1 2 +");
const char *p = s.c_str();
while (*p !=   )
{
while(isspace(*p)){
                   *p++;
      if(isdigit(*p))
      {
                     while(isdigit(*p))
                     {

                                 x+=*p++;
                                        cout << "digit is: "<< x<< endl;
            }

       }

       else{
            while(!isdigit(*p)&& !isspace(*p))
            x+=*p++;
            cout<<"other charcters are:"<<x<<endl;
            }
}
}
system("pause");
}

Not workingg

问题回答

你们可以使用直截了当的方法。

[...]
stringstream ss;
ss << s;
while(!ss.eof())
{
    char c = ss.peek(); // Looks at the next character without reading it
    if (isdigit(c))
    {
        int number;
        ss >> number;
        cout << "Digit is: " << number;
    }
    [...]
}

虽然其特性是空间(在isspace功能上加以验证)。

如果目前的性质是一位数,则目前的性质是一位数把它放在临时位置上。 当性质不再是数位数时,你有 > > /m>(可能只有一个 / 数>)。

Else if the character is not a digit or not a space, do the same as for numbers: collect into a temporary string and display when it ends.

Start over.

<><>Edit>/strong> 要求查阅的法典样本:

std::string expression = "10 20 +";
for (std::string::const_iterator c = expression.begin(); c != expression.end(); )
{
    std::string token;

    // Skip whitespace
    while (isspace(*c))
        c++;

    if (isdigit(*c))
    {
        while (isdigit(*c))
            token += *c++;
        std::cout << "Number: " << token << "
";
    }
    else
    {
        while (!isidigit(*c) && !isspace(*c))
            token += *c++;
        std::cout << "Other: " << token << "
";
    }
}




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...