How can I extract pairs of values from a string in C++ - c++

I have a string with this format:
"name1":1234 " name2 " : 23456 "name3" : 12345
and so on...
I have tried using nested while loops and two integers to store the position and length to use in string::substr, but I can't find a proper way to get it (most of the time I end up out of the string).
The values don't need to be stored, because I can call a function to process them as soon as I get them.
This is what I've done so far:
void SomeClass::processProducts(std::string str) {
unsigned int i = 0;
std::string name;
while (i < str.length()) {
if (str[i] == '\"') {
int j = 1;
while (str[i + j] != '\"') {
j++;
}
name = str.substr(i + 1, j - 1);
i += j;
}
else if (str[i] >= '0' && str[i] <= '9') {
int j = 1;
while (str[i + j] >= '0' && str[i + j] <= '9') {
j++;
}
//This is just processes the values
std::stringstream ss;
std::string num = str.substr(i, j);
ss.str(num);
int products = 0;
ss >> products;
if (products == 0) {
Util::error(ERR_WRONG_PRODUCTS);
}
int pos = getFieldPos(name);
if (pos == -1) {
Util::error(ERR_WRONG_NAME);
}
else {
fields[pos].addProducts(products);
}
i += j;
}
i++;
}
}
Thanks in advance.

Unfortunately, C++ doesn't have strong string parsing abilities out of the box. That's why there are many ways of doing these kinds of tasks.
However, there C++ does provide tools to help. So we can use them and at least avoid manual loops.
Before we begin I want to draw attention to the fact that when we are dealing with user input we must take extra care to validate the input.
The blocks that we need for the solution I chose are:
matching the format (with "name" : value). For this I chose std::find. Regex can also be used.
parsing the value into a number. For this we could use std::stoi. See bellow why it's not enough.
always make sure we are getting the input we expect. This adds some boilerplate code, but that's the price we have to pay. Also here we have a problem with std::stoi as it happily accepts trailing non whitespace without a fuss. So for instance 123 invalid would be parsed to 123. This is the reason I use a small wrapper around it parse_string_to_int
Ok, on we go:
Little helper:
auto parse_string_to_int(const std::string& str)
{
std::size_t num_processed = 0;
int val = std::stoi(str, &num_processed, 10);
auto next_non_space = std::find_if(str.begin() + num_processed, str.end(),
[](char ch) { return !std::isspace(ch); });
if (next_non_space != str.end())
throw std::invalid_argument{"extra trailing characters in parse_string_to_int"};
return val;
}
struct Product_token
{
std::string name;
int value;
};
auto get_next_product(std::string::const_iterator& begin, std::string::const_iterator end)
-> Product_token
{
// match `"name" : value "`
auto name_open_quote = std::find(begin, end, '\"');
auto name_close_quote = std::find(name_open_quote + 1, end, '\"');
auto colon = std::find(name_close_quote, end, ':');
auto next_token_open_quote = std::find(colon, end, '\"');
if (name_close_quote == end || name_close_quote == end || colon == end)
{
// feel free to add more information regarding the error.
// this is just the bare minimum to accept/reject the input
throw std::invalid_argument{"syntax error on parsing product"};
}
// advance to next token
begin = next_token_open_quote;
return Product_token{{name_open_quote + 1, name_close_quote},
parse_string_to_int({colon + 1, next_token_open_quote})};
}
auto process_products(const std::string& str)
{
auto begin = str.begin();
while (begin != str.end())
{
auto product = get_next_product(begin, str.end());
cout << '"' << product.name << "\" = " << product.value << endl;
}
}
int main()
{
auto str = R"("name1":1234 " name2 " : 23456 "name3" : 12345)"s;
try
{
process_products(str);
}
catch (std::exception& e)
{
cerr << e.what() << endl;
}
}
See full code in action on ideone

As long as you know the format, then extracting data is rather easy. First remove any quote or colon from the string and replace them with space. Now the string is delimited by space.
#include <iostream>
#include <iterator>
#include <string>
#include <algorithm>
#include <vector>
#include <sstream>
using namespace std;
int main()
{
string str("\"name1\":1234 \" name2 \" : 23456 \"name3\" : 12345");
cout << str << endl;
// remove ':' and '"' and replace them by space
std::replace_if(str.begin(), str.end(), ispunct, ' ');
istringstream ss(str);
vector<string> words;
// store data as name and number in vector<string>
copy(istream_iterator<string>(ss),istream_iterator<string>(),back_inserter(words));
for (int i(0); i < words.size(); i+=2)
cout << "name: " << words[i] << " number: " << words[i+1] << endl;
return 0;
}
The result is
"name1":1234 " name2 " : 23456 "name3" : 12345
name: name1 number: 1234
name: name2 number: 23456
name: name3 number: 12345

Related

what can I get sum that string tokens converted int in C++?

I need to sum 100, 200, 300 in a.txt
a.txt
2323|A|5|0|2|100
2424|B|6|1|3|200
2525|C|7|2|4|300
so I opened this file, and read line by line using getline(), and tokenized.
main.cpp
for (std::string each; std::getline(split, each, split_char); tokens.push_back(each)) {
for (int i = 0; i < tokens.size(); i++) {
std::cout << tokens[i] << std::endl;
tokens.pop_back();
}
}
As expected, that code printed singly of all things.
so I thought using token index to sum values. but my code have error.
"vector subscript out of range" or no compile.
first try
for (std::string each; std::getline(split, each, split_char); tokens.push_back(each)) {
for (int i = 0; i < tokens.size(); i++) {
std::cout << tokens[i] << std::endl;
tokens.pop_back();
std::cout << tokens[5] << std::endl;
std::cout << tokens[11] << std::endl;
std::cout << tokens[17] << std::endl;
int a = 0;
int b = 0;
int c = 0;
int sum = 0;
a = stoi(tokens[5]);
b = stoi(tokens[11]);
c = stoi(tokens[17]);
sum = (a + b + c);
std::cout << sum << std::endl;
}
}
second try
for (std::string each; std::getline(split, each, split_char); tokens.push_back(each)) {
if(tokens.size() > 4) {
for (int k = 0; k < ((tokens.size() - 5) / 6) + 1; k++) {
int sum = 0;
int change = 0;
int j = 0;
j = 6 * k + 5;
change = stoi(tokens[j]);
sum += change;
std::cout << sum << std::endl;
tokens.pop_back();
}
}
}
what should I do sum value? and I'm wondering that tokens.size()`s meaning except meaning "size" because second for statement always get an error if not exactly correcting i < tokens.size()
You are modifying the tokens vector while you are looping through it. Don't do that. You are affecting its size(), which accounts for why you are able to go out of bounds.
You say that you need to sum only the last token of each line. But that is not what your code is trying to do. There is no need for an inner for loop at all. Simply split each line into a local tokens vector and then use tokens.back() to get the last token, eg:
std::string line;
int sum = 0;
while (std::getline(inFile, line))
{
std::istringstream iss(line);
std::vector<std::string> tokens;
std::string token;
while (std::getline(iss, token, '|')) {
tokens.push_back(token);
}
// use tokens as needed...
token = tokens.back();
sum += std::stoi(token);
}
std::cout << sum << std::endl;
Live Demo
I would like to structure my code slightly differently.
Rather than try and do everything in the main function split your code up so that you read each line and validate it is correct:
#include <iostream>
#include <string>
// A structure representing the data we want to parse.
struct DataLine
{
int v1;
char c;
int v2;
int v3;
int v4;
int v5;
// An input operator that will read one line of data.
// If the data is valid will update the variable we are reading into.
friend std::istream& operator>>(std::istream& str, DataLine& data)
{
DataLine tmp;
char s[5];
std::string extra;
if ( str >> tmp.v1 >> s[0] && s[0] == '|'
&& str >> tmp.c >> s[1] && s[1] == '|'
&& str >> tmp.v2 >> s[2] && s[2] == '|'
&& str >> tmp.v3 >> s[3] && s[3] == '|'
&& str >> tmp.v4 >> s[4] && s[4] == '|'
&& str >> tmp.v5
&& std::getline(str, extra) && extra.empty())
{
// all the data was read and the line was valid.
// update the correct variable.
swap(tmp, data);
}
else {
// there was an issue.
// set the stream to bad so that reading will stop.
str.setstate(std::ios::badbit);
}
return str;
}
// Standard swap method.
friend void swap(DataLine& lhs, DataLine& rhs) noexcept
{
using std::swap;
swap(lhs.v1, rhs.v1);
swap(lhs.c , rhs.c );
swap(lhs.v2, rhs.v2);
swap(lhs.v3, rhs.v3);
swap(lhs.v4, rhs.v4);
swap(lhs.v5, rhs.v5);
}
};
Then the loop you use to read the data becomes really trivial to implement.
int main()
{
DataLine data;
int sum = 0;
// Now we can read the data in a simple loop.
while(std::cin >> data) {
sum += data.v5;
}
std::cout << "Sum: " << sum << "\n";
}

Finding the middle word in a string

As the title suggests, I'm having trouble trying to grab the middle word out of a string, I believe my formula is wrong but I'm not completely sure where to from here to fix the issue, any help is always appreciated thank you!
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string sentence="";
string middle="";
string midtemp="";
int count=0;
int mid=0;
cout << "Enter a sentence:" << endl;
getline(cin,sentence); //gets user input
for(int count =0; count<sentence.length();count++){
letter=sentence.substr(count,1);
int mid = sentence.length();
if (midtemp.length()>middle.length())
{ midtemp=middle;}
if (sentence[count]!=' ')
{ if(mid%2==0);
reverse(longest.rbegin(),longest.rend()); //shows the word not backwards
cout<<"Middle word is: " << sentence.substr(mid/2 -1) <<"\n" << endl;
break; //presents info to user
}
else(mid%2!=0);
{ mid/2;
cout<<"Middle word is: " << sentence.substr(mid/2 -1) <<"\n" << endl;
break; //presents info to user if number is even
}
}
To find the middle word in a string with three words use:
size_t begin_index = sentence.find(' ') + 1;
size_t end_index = sentence.find(' ', begin_index);
size_t length = end_index - begin_index;
string middle_word = sentence.substr(begin_index, length);
To find the middle word in a string with any odd number of words use:
// create string stream from sentence
istringstream ss(sentence);
// split string stream into vector of words
vector<string> words(istream_iterator<string>(ss), {});
// get middle index
size_t middle_index = (words.size() - 1) / 2;
// get middle word
const auto& middle_word = words[middle_index];
If there are an even number of words, the result is either rounded up or down until C++11, after C++11 it is rounded down. (To the word before the middle space).
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char **argv) {
string input;
getline(cin, input);
vector<string> tokens;
string token;
for (size_t i = 0; i < input.length(); i++) {
char c = input[i];
if (c == ' ' || !input[i + 1]) {
if (!input[i + 1])
token += c;
tokens.push_back(token);
token = "";
continue;
}
token += c;
}
auto mid = tokens.size() % 2 == 0 ? tokens.begin() + tokens.size() / 2 - 1
: tokens.begin() + tokens.size() / 2;
cout << *mid;
return 0;
}

I need to convert some code so that it works with an input and output file text

I have a program that reverses the letters in a sentence but keeps the words in the same order. I need to change the code from an iostream library to an fstream library where the user inputs a sentence into an input file("input.txt") and the program outputs the reverse into an output text file.
example of input:
This problem is too easy for me. I am an amazing programmer. Do you agree?
Example of output:
sihT melborp si oot ysae rof em. I ma na gnizama remmargorp. oD uoy eerga?
The code I already have:
int main()
{
int i=0, j=0, k=0, l=0;
char x[14] = "I LOVE CODING";
char y[14] = {'\0'};
for(i=0; i<=14; i++) {
if(x[i]==' ' || x[i]=='\0') {
for(j=i-1; j>=l; j--)
y[k++] = x[j];
y[k++] = ' ';
l=i+1;
}
}
cout << y;
return 0;
}
I would use std::string to store the string, and benefit from std::vector and const_iterator to make better use of C++ features:
#include <string>
#include <vector>
int main()
{
std::string s("This problem is too easy for me. I am an amazing programmer. Do you agree?");
const char delim = ' ';
std::vector<std::string> v;
std::string tmp;
for(std::string::const_iterator i = s.begin(); i <= s.end(); ++i)
{
if(*i != delim && i != s.end())
{
tmp += *i;
}else
{
v.push_back(tmp);
tmp = "";
}
}
for(std::vector<std::string>::const_iterator it = v.begin(); it != v.end(); ++it)
{
std::string str = *it,b;
for(int i=str.size()-1;i>=0;i--)
b+=str[i];
std::cout << b << " ";
}
std::cout << std::endl;
}
Output:
sihT melborp si oot ysae rof .em I ma na gnizama .remmargorp oD uoy ?eerga
The code that you submitted looks much more like something from C rather than from C++. Not sure if you are familiar std::string and function calls. As the code you wrote is pretty sophisticated, I will assume that you are.
Here is an example of how to use fstream. I almost always you getline for the input because I find that it gets me into fewer problems.
I then almost always use stringstream for parsing the line because it neatly splits the lines at each space.
Finally, I try to figure out a while() or do{}while(); loop that will trigger off of the input from the getline() call.
Note that if the word ends in a punctuation character, to keep the punctuation at the end, the reverse_word() function has to look for non-alpha characters at the end and then save that aside. This could be done by only reversing runs of alphas.
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
///////////////////
/// return true if ch is alpha
/// return false for digits, punctuation, and all else
bool is_letter(char ch){
if((ch >= 'A' && ch <= 'Z') ||
(ch >= 'a' && ch <= 'z')) {
return true;
} else {
return false;
}
}
////////
// Only reverse the letter portion of each word
//
std::string reverse_word(std::string str)
{
std::string output_str; // Probably have to create a copy for output
output_str.reserve(str.length()); // reserve size equal to input string
// iterate through each letter of the string, backwards,
// and copy the letters to the new string
char save_non_alpha = 0;
for (auto it = str.rbegin(); it != str.rend(); it++) {
/// If the last character is punctuation, then save it to paste on the end
if(it == str.rbegin() && !is_letter(*it)) {
save_non_alpha = *it;
} else {
output_str += *it;
}
}
if(save_non_alpha != 0) {
output_str += save_non_alpha;
}
return output_str; // send string back to caller
}
int main()
{
std::string input_file_name{"input.txt"};
std::string output_file_name{"output.txt"};
std::string input_line;
std::ifstream inFile;
std::ofstream outFile;
inFile.open(input_file_name, std::ios::in);
outFile.open(output_file_name, std::ios::out);
// if the file open failed, then exit
if (!inFile.is_open() || !outFile.is_open()) {
std::cout << "File " << input_file_name
<< " or file " << output_file_name
<< " could not be opened...exiting\n";
return -1;
}
while (std::getline(inFile, input_line)) {
std::string word;
std::string sentence;
std::stringstream stream(input_line);
// I just like stringstreams. Process the input_line
// as a series of words from stringstream. Stringstream
// will split on whitespace. Punctuation will be reversed with the
// word that it is touching
while (stream >> word) {
if(!sentence.empty()) // add a space before all but the first word
sentence += " ";
word = reverse_word(word);
sentence += word;
}
outFile << sentence << std::endl;
}
inFile.close();
outFile.close();
return 0;
}

print 2nd word in a string with its size in C++

I am trying to make a program in which a user enters a string and i will print out the second word in the string with its size.
The delimiter's are space( ), comma(,) and tab( ).
I have used a character array and fgets to read from user and a character pointer that points to the first element of the array.
source code:
#include"iostream"
#include<stdio.h>
#include<string>
using namespace std;
// extract the 2nd word from a string and print it with its size(the number of characters in 2nd word)
int main()
{
char arr[30], arr1[30];
char *str = &arr1[0];
cout<<"Enter a string: ";
fgets(str, 30, stdin);
int i = 0, j, count = 1, p = 0; // count is used to find the second word
// j points to the next index where the first delimiter is found.
// p is used to store the second word found in character array 'arr'
while(*(str+i) != '\n')
{
if(*(str+i) == ' ' || *(str+i) == ',' || *(str+i) == ' ')
{
count++;
if(count == 2)
{
// stroing 2nd word in arr character array
j = i+1;
while(*(str+j) != ' ' || *(str+j) != ',' || *(str+j) != ' ')
{
arr[p] = *(str+j);
cout<<arr[p];
p++;
i++;
j++;
}
break;
}
}
i++;
}
arr[p+1] = '\0'; // insert NULL at end
i = 0;
while(arr[i] != '\0')
{
cout<<arr[i];
i++;
}
cout<<"("<<i<<")"<<endl;
return 0;
}
Help me out with this.
To start, don't use std::cin for testing. Just set a value in your code for consistency and ease of development. Use this page for a reference.
#include <iostream>
#include <string>
int main() {
std::string str("this and_that are the tests");
auto start = str.find_first_of(" ,\n", 0);
auto end = str.find_first_of(" ,\n", start + 1);
std::cout << str.substr(start, end - start);
return 0;
}
And this is still somewhat of a hack, it just depends where you are going. For instance the Boost library is rich with extended string manipulation. If you are going to parse out more than just one word it can still be done with string manipulations, but ad-hoc parsers can get out of hand. There are other tools like Boost Spirit to keep code under control.
The delimiters used when extracting from a stream depends on the locale currently in effect. One (cumbersome) way to change the extraction behaviour is to create a new locale with a special facet in which you specify your own delimiters. In the below example the new locale is used to imbue a std::stringstream instead of std::cin directly. The facet creation part is mostly copy/paste from other answers here on SO, so you'll find plenty of other examples.
#include <iostream>
#include <locale> // std::locale, std::ctype<char>
// https://en.cppreference.com/w/cpp/locale/ctype_char
#include <sstream> // std::stringstream
#include <algorithm> // std::copy_n
#include <vector> // a container to store stuff in
// facet to create our own delimiters
class my_facet : public std::ctype<char> {
mask my_table[table_size];
public:
my_facet(size_t refs = 0)
: std::ctype<char>(&my_table[0], false, refs)
{
// copy the "C" locales table to my_table
std::copy_n(classic_table(), table_size, my_table);
// and create our delimiter specification
my_table[' '] = (mask)space;
my_table['\t'] = (mask)space;
my_table[','] = (mask)space;
}
};
int main() {
std::stringstream ss;
// create a locale with our special facet
std::locale loc(std::locale(), new my_facet);
// imbue the new locale on the stringstream
ss.imbue(loc);
while(true) {
std::string line;
std::cout << "Enter sentence: ";
if(std::getline(std::cin, line)) {
ss.clear(); // clear the string stream from prior errors etc.
ss.str(line); // assign the line to the string stream
std::vector<std::string> words; // std::string container to store all words in
std::string word; // for extracting one word
while(ss>>word) { // extract one word at a time using the special facet
std::cout << " \"" << word << "\" is " << word.size() << " chars\n";
// put the word in our container
words.emplace_back(std::move(word));
}
if(words.size()>=2) {
std::cout << "The second word, \"" << words[1] << "\", is " << words[1].size() << " chars\n";
} else {
std::cout << "did not get 2 words or more...\n";
}
} else break;
}
}
#include"iostream"
#include<stdio.h>
#include<string>
#include <ctype.h>
using namespace std;
int main()
{
char c;
string str;
char emp = ' ';
cout<<"Enter a string: ";
getline (cin,str);
int j = 0, count = 1, counter = 0;
for (int i = 0; i < str.length() && count != 2; i++)
{
cout<< str[i] <<endl;
if( isspace(str[i]) || str[i] == ',' || str[i] == '\t' )
{
count++;
if(count == 2)
{
j = i+1;
while(j < str.length())
{
if (isspace(str[j]) || str[j] == ',' || str[j] == '\t')
{
break;
}
cout<<str[j];
counter++;
j++;
}
cout<<endl;
}
}
}
cout<<"size of the word: "<<counter<<endl;
return 0;
}
This is a simple answer to what you want, hope to help you.
// Paul Adrian P. Delos Santos - BS Electronics Engineering
// Exercise on Strings
#include <iostream>
#include <sstream>
using namespace std;
int main(){
// Opening Message
cout << "This program will display the second word and its length.\n\n";
// Ask for a string to the user.
string input;
cout << "Now, please enter a phrase or sentence: ";
getline(cin, input);
// Count the number of words to be used in making a string array.
int count = 0;
int i;
for (i=0; input[i] != '\0'; i++){
if (input[i] == ' ')
count++;
}
int finalCount = count + 1;
// Store each word in a string array.
string arr[finalCount];
int j = 0;
stringstream ssin(input);
while (ssin.good() && j < finalCount){
ssin >> arr[j];
j++;
}
// Display the second word and its length.
string secondWord = arr[1];
cout << "\nResult: " << arr[1] << " (" << secondWord.size() << ")";
return 0;
}

Scan string and store numbers and operations in different vectors C++

Suppose I have a string exp in the following format:
123+456*789-1011+1213
I want to store all the numbers in vector numbers, and all the operations in vector op.
vector<long long>numbers // {123, 456, 789, 1011, 1213}
vector<char>op // {+, *, -, +}
for (int i = 0; i < exp.size(); i++){
if (exp[i]=="+" || exp[i]=="-" || exp[i]=="*"){
op.push_back(exp[i]);
}else{
...
}
}
How do I store the numbers, and convert them from char to long long?
You will need to parse the input expression to extract the numbers and operator.
There are many ways by which this can be done, however following is my approach.
Traverse through all the character and push the values in Operator vector which is not a digit and replace it with space.
Now extract the numbers from the expression and convert it to numbers and push the values in Number vector.
To know how to split a string you can check the following links:
Split a string - Stack overflow
Split a string - cplusplus.com
Use stol or strtol or string stream to convert string to long value.
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
int main()
{
std::string exp = "123+456*789-1011+1213";
std::vector<long> vecNums;
std::vector<char> vecOper;
for (decltype(exp.size()) i = 0; i < exp.size(); ++i) {
if (!isdigit(exp[i])) {
vecOper.push_back(exp[i]);
exp[i] = ' ';
}
}
std::istringstream iss(exp);
while (iss) {
std::string substr;
long num;
std::getline(iss, substr, ' ');
if (substr.size() != 0) {
// Using strtol function
// num = strtol(substr.c_str(), NULL, 10);
// Using stol function
num = stol(substr);
vecNums.push_back(num);
}
//
// Or use string stream to convert string to long
//
//long num;
//iss >> num;
//vecNums.push_back(num);
}
std::cout << "Numbers: " << std::endl;
for (auto &i : vecNums) {
std::cout << i << " ";
}
std::cout << "\nOperators: " << std::endl;
for (auto &i : vecOper)
std::cout << i << " ";
return 0;
}
If you're going to use iostreams:
void parse_string(const string& s) {
using num_t = long long;
using op_t = char;
istringstream sstr(s);
vector<num_t> numbers;
vector<op_t> ops;
(void)sstr.peek(); //set eofbit if s.empty()
while (!sstr.eof() && !sstr.fail()) {
num_t num;
sstr >> num;
if (!sstr.fail()) {
numbers.push_back(num);
if (!sstr.eof()) {
op_t op;
sstr >> op;
if (!sstr.fail()) {
ops.push_back(op);
}
}
}
}
//assert(ops.size() + 1 == numbers.size());
//copy(begin(numbers), end(numbers), ostream_iterator<num_t>(cout, " "));
//copy(begin(ops), end(ops), ostream_iterator<op_t>(cout, " "));
}
Error checking code as been removed (validate operators are correct, exceptions).