answersLogoWhite

0

#include<iostream>

#include<sstream>

using namespace std;

string digits {"0123456789+-.,"};

bool is_number (const string& str)

{

if (str.find_first_not_of(digits) != str.npos)

return false;

// If a plus sign is present, it must be the first character.

size_t f = str.rfind ('+');

if (f!=0 && f!=str.npos)

return false;

// If a minus sign is present, it must be the first character.

f = str.rfind ('-');

if (f!=0 && f!=str.npos)

return false;

// There can only be one decimal point at most.

size_t dot = str.find ('.');

if (dot != str.rfind ('.'))

return false;

// If a decimal point is present, it must not be the last character.

if (dot str.npos && comma != str.npos && (str.size()-comma)%4)

return false;

return true;

}

string get_first_number (const string& str)

{

for (auto b=str.begin(); b!=str.end(); ++b)

{

if (digits.find (*b) != digits.npos)

{

for (auto e=str.rbegin(); e.base()!=b; ++e)

{

if (digits.find (*e) != digits.npos)

{

auto t = e.base();

string n = str.substr (b-str.begin(), t-b);

if (is_number (n))

return n;

}

}

}

}

return "";

}

int main()

{

string text {"This string has the numbers 42, 1,000,000, 5,0, +1, -2, 3.14 and .5."};

string number;

cout << "Original string: "" << text << """ << endl;

cout << "\nNumbers:\n";

while ((number=get_first_number (text)).size())

{

cout << number << endl;

size_t pos = text.find (number);

text.replace (pos, number.size(), "\n");

}

cout << "\nStrings:\n";

stringstream ss;

ss << text << '\n';

while (!ss.eof())

{

getline (ss, text, '\n');

if (text.size())

cout << '"' << text << '"' << endl;

}

cout << endl;

}

User Avatar

Wiki User

10y ago

What else can I help you with?

Continue Learning about Engineering

What is the sort order for alphanumeric fields that contain both numbers and letters?

It depends on the collating order of the character set being used by the computer:ASCII places the numbers before the lettersEBCDIC places the numbers after the lettersFIELDATA places the numbers after the lettersetc.Some early computers had a different collating order than the numeric order of the character codes in the character set, but for modern computers the collating order is usually identical to the numeric order of the character codes.


What lets you define a color with numbers and letters?

The hexadecimal system


What type of data allows letters and numbers?

Character(-string).


Types of data type?

A data type that can store integer numbers. The details vary depending on the programming language; many language have different integer types to accomodate different sizes. This lets the programmer use a smaller size (and save memory space) when he only needs to store fairly small numbers - and especially when he needs to store LOTS of fairly small numbers, as in an array. Common sizes include 1 byte, 2 bytes, 4 bytes and 8 bytes of storage.


What are the rules of creating identifier?

Identifiers refers to the names of variables, functions and array. These are user defined names and consist of a sequence of letters and digits, with a letters as a first character.Both uppercase and lowercase letters are permitted, although lowercase letters are commonly used. The underscore character is also permitted in identifiers. It is usually used as a link between two words in long identifiers. In C, identifiers may contain any alphanumeric characters (a-z, A-Z, 0-9) as well as underscores (_), but must not begin with a number.