C++ Count even numbers in a string [duplicate] - c++

This question already has answers here:
std::cin.getline( ) vs. std::cin
(5 answers)
C++ "cin" only reads the first word [duplicate]
(5 answers)
Closed 2 years ago.
I have a script here that counts even numbers in an int type var.
#include <iostream>
#include <string>
int main()
{
std::cout << "Type a string: " << std::endl;
std::string s;
std::cin >> s;
unsigned int digits = 0, evens = 0;
for ( std::string::size_type i = 0; i < s.size() && s[i] >= '0' && s[i] <= '9'; i++ )
{
++digits;
evens += ( s[i] - '0' ) % 2 == 0;
}
std::cout << "The number has " << digits << " digit(s)." << std::endl;
std::cout << "The number has " << evens << " even digit(s)." << std::endl;
return 0;
}
Im trying to find a way to turn this into a string instead so I can count on how many even numbers or numbers are there in that string?
Type a string:
29 coaches 28
Even: 1
Found Numbers: 1
In python it should be something like:
s = "75,41,14,8,73,45,-16"
evenNumbers = []
for number in s.split(","):
int_num = int(number)
if int_num % 2 == 0 and int_num > 0:
evenNumbers.append(int_num)
print("Even Numbers : \"{}\"".format(evenNumbers))
But I dont know how to do it in C++

Your loop will stop on first non-numeric character: i < s.size() && s[i] >= '0' && s[i] <= '9'
Also, you are counting also alphanumerics too. So your loop needs fixing first. I recommend using regex for such problems it will be much generic:
const std::regex numbers("\\-?\\d+");
for (auto it = std::sregex_iterator(s.begin(), s.end(), numbers); it != std::sregex_iterator(); it++) {
++digits;
char lastCharacterOfMatchingString = *it->str(0).rbegin();
evens += (lastCharacterOfMatchingString - '0') % 2 == 0;
}

This sounds like 3 requirements:
split a string based on a space: see How do I iterate over the words of a string?
try convert the words to numbers - can use std::stoi() for this
count the even numbers - can use modulus for this
So something like:
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iterator>
int main()
{
//0 - Get input
std::cout << "Type a string: " << std::endl;
std::string s;
std::getline(std::cin, s) ;
//1 - split
std::istringstream iss(s);
std::vector<std::string> words{std::istream_iterator<std::string>{iss}, std::istream_iterator<std::string>{}};
std::vector<int> res; //store the results as per the python example
for(auto it: words){
int val = 0;
try{
val = std::stoi(it, nullptr); //2 - convert
}
catch (const std::invalid_argument& ia) { /* what to do with words */ }
if ((val != 0) && (val % 2 == 0))
res.push_back(val); //3 - count
}
std::cout << "even numbers: " << std::to_string(res.size()) << std::endl;
return 0;
}

Related

c++ count words in array

I need to write a function that gets a string and count how many words there are in the string and how many letters. And then calculate the average of it.
A word in a string is a sequence of letters and numbers separated by one or more spaces.
First of all I have to check if the string is correct. The string must contain only lowercase letters, uppercase letters, and numbers only.
i didnt menage to count all sort of words correctly and also my function doesnt count the last letter.
#include <iostream>
using namespace std;
#include <string.h>
#define SIZE 50
float checkString(char string[]) {
float wordCounter = 0;
float letterCounter = 0;
bool isLegit = true;
int i = 0;
while (isLegit) {
if (((string[i] >= 48 && string[i] <= 57) ||
(string[i] >= 65 && string[i] <= 90) ||
(string[i] >= 97 && string[i] <= 122 ))) {
for (int j = 0; j <= strlen(string); j++) {
if ((string[j - 1] != ' ' && string[j] == ' ' &&
string[i + 1] != ' ')
|| j == (strlen(string) - 1)) {
wordCounter++;
}
else if (string[j] != ' ') {
letterCounter++;
cout << string[j];
}
}
cout << " The avareage is : " << (letterCounter /
wordCounter) << endl;
isLegit = false;
}
else {
return -1;
isLegit = false;
}
}
cout << "Number of words " << wordCounter << endl;
cout << "Number of letters " <<letterCounter << endl;
}
int main() {
char string[SIZE];
cout << "please enter a sentence " << endl;
cin.getline(string, SIZE);
checkString(string);
}
Instead of using char[] for strings, I suggest that you use std::string which can grow and shrink dynamically. It's one of the most common types to use in the standard C++ library. You can also make use of stringstreams which lets you put a string inside it and then you can extract the contents of the stringstream using >>, just like when reading from std::cin.
Example with comments in the code:
#include <iostream>
#include <sstream> // std::stringstream
#include <string> // std::string
// use std::string instead of a char[]
float checkString(const std::string& string) {
// put the string in a stringstream to extract word-by-word
std::istringstream is(string);
unsigned words = 0;
unsigned letters = 0;
std::string word;
// extract one word at a time from the stringstream:
while(is >> word) {
// erase invalid characters:
for(auto it = word.begin(); it != word.end();) {
// Don't use magic numbers. Put the character literals in the code so
// everyone can see what you mean
if((*it>='0' && *it<='9')||(*it>='A' && *it<='Z')||(*it>='a' && *it<='z')) {
// it was a valid char
++it;
} else {
// it was an invalid char, erase it
it = word.erase(it);
}
}
// if the word still has some characters in it, make it count:
if(word.size()) {
++words;
letters += word.size();
std::cout << '\'' << word << "'\n"; // for debugging
}
}
std::cout << "Number of words " << words << "\n";
std::cout << "Number of letters " << letters << "\n";
std::cout << "The average number of letters per word is "
<< static_cast<float>(letters) / words << '\n';
return 0.f; // not sure what you are supposed to return, but since the function
// signature says that you should return a float, you must return a float.
}
int main() {
checkString(" Hello !!! World, now let's see if it works. ");
}
I would like to add an additional answer. This answer is based on "more-modern" C++ and the usage of algorithms. You want to solve 3 tasks:
Check, if string is OK and matched to your expectations
Count the number of words in the given string
Count the number of letters
Calculate the ratio of words/letters
For all this you may use existings algorithms from the C++ standard library. In the attached example code, you will see a one-liner for each task.
The statements are somehow very simple, so that I will not explain much more. If there should be a question, I am happy to answer.
Please see here one possible example code:
#include <iostream>
#include <string>
#include <iterator>
#include <regex>
#include <algorithm>
#include <tuple>
#include <cctype>
std::regex re("\\w+");
std::tuple<bool, int, int, double> checkString(const std::string& str) {
// Check if string consists only of allowed values, spaces or alpha numerical
bool stringOK{ std::all_of(str.begin(), str.end(), [](const char c) { return std::isalnum(c) || std::isspace(c); }) };
// Count the number of words
int numberOfWords{ std::distance(std::sregex_token_iterator(str.begin(),str.end(), re, 1), {}) };
// Count the number of letters
int numberOfLetters{ std::count_if(str.begin(), str.end(), isalnum) };
// Return all calculated values
return std::make_tuple(stringOK, numberOfWords, numberOfLetters, static_cast<double>(numberOfWords)/ numberOfLetters);
}
int main() {
// Ask user to input string
std::cout << "Please enter a sentence:\n";
// Get string from user
if (std::string str{}; std::getline(std::cin, str)) {
// Analyze string
auto [stringOk, numberOfWords, numberOfLetters, ratio] = checkString(str);
// SHow result
std::cout << "\nString content check: " << (stringOk ? "OK" : "NOK") << "\nNumber of words: "
<< numberOfWords << "\nNumber of letters: " << numberOfLetters << "\nRatio: " << ratio << "\n";
}
return 0;
}
Of course there are many more other possible solutions. But, because of the simplicity of this solution, I showed this variant.

Perform operation when using str.find() on string c++ (no sub string) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a problem when checking for commas in a .csv-file. For each row i want to perform an operation when the 10th comma is found and check the value after that comma. This value is always a number less than 9.
int main()
{
string row;
ifstream infile;
infil.open ("file.csv");
int sum = 0;
while(getline(infil,row))
{
for(int i = 0; i < row.size() ;i++)
{
if(row.find(',') != std::string::npos)
{
sum++;
}
if(sum == 10)
{
//PERFORM OPERATION
}
}
}
return 0;
}
The code that i have written does not work, any help?
You can use something like this:
#include <iostream>
#include <fstream>
#include <string>
using std::string;
using std::ifstream;
using std::cout;
using std::cerr;
using std::endl;
int main()
{
ifstream infile;
//infile.open("file.csv");
infile.open("C:\\Users\\Kraemer\\Desktop\\test.csv");
string row;
while (getline(infile, row))
{
int sum = 0; //number of commas
size_t pos = 0; //Position in row
//As long as we didn't find 10 commas AND there is another comma in this line
while(sum < 10 && (pos = row.find(',', pos)) != string::npos)
{
//Comma found
sum++;
//Set position behind the comma
pos++;
}
//When we come here sum is always <= 10
if(sum == 10)
{ //10 commas found
cerr << "Found 10 commas" << endl;
}
else
{
cerr << "Did not find enough commas in line" << endl;
}
}
return 0;
}
You should also notice that getline(infile, row) will also fail when the EOF is in the last line that contains data.
So you either need to check the last read line when infile.eof() returns true or make sure that the input data ends with an empty line.
To extract the digit after the tenth comma you can do something like this:
if (sum == 10)
{ //10 commas found
cerr << "Found 10 commas" << endl;
if (pos < row.size())
{
char digitAsChar = row[pos];
if (digitAsChar >= '0' && digitAsChar <= '9') // Valid digit
{
int digitAsInt = digitAsChar - '0'; //Convert from char to int
cout << "Found digit " << digitAsInt << endl;
}
else
{
cerr << "Character '" << digitAsChar << "' is no digit." << endl;
}
}
else
{
cerr << "10th comma is at the end of the line - no digit found" << endl;
}
}
else
{
cerr << "Did not find enough commas in line" << endl;
}
Input:
,,,,,,,,,,1
,,,,,,,,,,2
,,,,,,,,,,3
,,,,,,,,,,4
,,,,,,,,,,5
,,,,,,,,,,f
,,,,,,,,,,
,,,,,,,,,,8
,,,,,,,,,9
,,,,,,,10
Output:
Found 10 commas
Found digit 1
Found 10 commas
Found digit 2
Found 10 commas
Found digit 3
Found 10 commas
Found digit 4
Found 10 commas
Found digit 5
Found 10 commas
Character 'f' is no digit.
Found 10 commas
10th comma is at the end of the line - no digit found
Found 10 commas
Found digit 8
Did not find enough commas in line
Did not find enough commas in line
Try something like that
int main()
{
string row;
ifstream infile;
infile.open ("file.csv");
int sum = 0;
while(getline(infile,row))
{
// sum = 0; // you can reset sum at start of loop if you want,
// or continue to use sum from last iteration
int pos = row.find(','); // find first comma
while (pos != std::string::npos)
{
if (++sum == 10)
{
//PERFORM OPERATION
sum = 0; // reset comma count if you want
}
pos = row.find(',', ++pos); // find next comma (after pos)
}
}
return 0;
}
Here's a somewhat minimal implementation (not including reading from file):
#include <iostream>
#include <string>
int main() {
const std::string row = "a,b,c,d,e,f,g,h,i,j,7,k,l";
size_t commas = 0;
std::string::size_type pos = 0;
while (++commas <= 10) {
pos = row.find(',', pos);
if (pos == std::string::npos) {
// Out of commas
break;
} else {
++pos;
}
}
if (pos != std::string::npos) {
int number = std::stoi(row.substr(pos, 1) );
std::clog << "number = " << number << std::endl;
}
}
Output:
number = 7
The trick is to keep track of the position of the last found comma (which starts out as 0 before any comma has been found) and using it for the second argument to std::string::find().
To expand into a ‘real’ implementation one should wrap the std::stoi() call in a try and in the catch do whatever needs done if whatever comes after the tenth comma isn't a number (or too large). And possibly put an else after the last block and do whatever needs done when there aren't ten commas in row (or the tenth comma is the last character).
A simple solution without using string::find could be like this:
int main() {
string s = "1,2,3,4,5,6,7,8,9,10,11";
int n = 0;
int i = 0;
for(auto c : s)
{
if (c == ',')
{
++n;
if (n == 10) break;
}
++i;
}
if (n == 10)
{
string tmp = s.substr(i+1);
cout << "Rest of string is: " << tmp << endl;
}
else
{
cout << "Only found " << n << endl;
}
return 0;
}

Read digits from an int and counting them

Alright so I've been looking though Google and on forums for hours and can't seem to understand how to solve this problem.
I need to write a program that first determines if the number entered by the user is a base 5 number (in other words, a number that only has 0s, 1s, 2s, 3s, and 4s in it). Then, I have to count how many 0s, 1s, 2s, etc are in the number and display it to the user.
I've seen people saying I should convert int to a string and then using cin.get().
I noticed that I can't use cin.get() on a string, it needs to be a char.
I can only use a while loop for this assignment, no while... do loops.
Any help is appreciated!!
Here's what I have so far, obviously with all my mistakes in it:
//----------------------------------------------
// Assignment 3
// Question 1
// File name: q1.cpp
// Written by: Shawn Rousseau (ID: 7518455)
// For COMP 218 Section EC / Winter 2015
// Concordia University, Montreal, QC
//-----------------------------------------------
// The purpose of this program is to check if the
// number entered by the user is a base of 5
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
// Declaring variables
int number;
int zeros;
int ones;
int twos;
int threes;
int fours;
bool base5;
// Get data and calculate
cin >> number;
string numberString = to_string(number);
// Determine if the number is a base 5 number
while (cin.get(numberString) == 0 || cin.get(numberString) == 1 ||
cin.get(numberString) == 2 || cin.get(numberString) == 3 ||
cin.get(numberString) == 4)
base5 = true;
// Determine the number of each digits
zeros = 0;
ones = 0;
twos = 0;
threes = 0;
fours = 0;
return 0;
}
Several things you need to beware of:
One way to get a specific character from a std::string is by []. e.g.
std::string myString{"abcdefg"};
char myChar = myString[4]; // myChar == 'e'
cin.get(aString) is not trying to get data from aString. It continues to get data from stdin and store in aString. Once you have get the data and put into the string, you can simply manipulate the string itself.
a short piece of code that will count number of vowel in a string. If you can understand it, there should be no problem doing your work. (Haven't compiled, probably some typos)
std::string inputString;
std::cin >> inputString;
// you said you need a while loop.
// although it is easier to with for loop and iterator...
int i = 0;
int noOfVowels = 0;
while (i < inputString.length()) {
if (inputString[i] == 'a'
|| inputString[i] == 'e'
|| inputString[i] == 'i'
|| inputString[i] == 'o'
|| inputString[i] == 'u' ) {
++noOfVowels;
}
++i;
}
std::cout << "number of vowels : " << noOfVowels << std::endl;
Well you can try this approach. This will solve your needs I guess.
#include <iostream>
#include <string>
#include <sstream>
#include <conio.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int number=0;
int zeros=0;
int ones=0;
int twos=0;
int threes=0;
int fours=0;
bool valid=true;;
int counter = 0;
cout<<"Enter the number: ";
cin >> number;
stringstream out;
out << number; //int to string
string numberString = out.str();
cout<<"\n\nNumber after string conversion : "<<numberString;
cout<<"\nPrinting this just to show that the conversion was successful\n\n\n";
while (counter < numberString.length())
{
if (numberString[counter] == '0')
zeros++;
else if(numberString[counter] == '1')
ones++;
else if(numberString[counter] == '2')
twos++;
else if(numberString[counter] == '3')
threes++;
else if(numberString[counter] == '4')
fours++;
else
valid=false;
counter++;
}
if(valid==true)
{
cout<<"\nZeros : "<<zeros;
cout<<"\nOnes : "<<ones;
cout<<"\nTwos : "<<twos;
cout<<"\nThrees : "<<threes;
cout<<"\nFours : "<<fours;
}
else
cout<<"\n\nInvalid data...base of 5 rule violated";
_getch();
return 0;
}
If you don't want to use std::string then use characters, first loop over the input from the user until ENTER is pressed.
char ch = 0;
while ((ch = cin.get()) != '\n')
{
...
}
For each character read, check if it is a digit (std::isdigit) and if it is in the range 0..4, if not quit and give some message of not being base 5
have an array of ints to keep track of the frequency of the digits
int freq[5] = {0,0,0,0,0};
after you have checked that the character is valid subtract the ascii value from the digit and use that as index in the array, increment that:
freq[ch - '0']++;
e.g.
char ch;
int freq[5] = {0};
while ((ch = cin.get()) != '\n')
{
cout << ch;
freq[ch-'0']++;
}
for (int i = 0; i < sizeof(freq)/sizeof(freq[0]); ++i)
{
cout << static_cast<char>(48+i) << ":" << freq[i] << endl;
}
Here's a useful function that counts digits:
// D returns the number of times 'd' appears as a digit of n.
// May not work for n = INT_MIN.
int D(int n, int d) {
// Special case if we're counting zeros of 0.
if (n == 0 && d == 0) return 1;
if (n < 0) n = -n;
int r = 0;
while (n) {
if (n % 10 == d) r++;
n /= 10;
}
return r;
}
In your code you can use this naively to solve the problem without any further loops.
if (D(n, 5) + D(n, 6) + D(n, 7) + D(n, 8) + D(n, 9) != 0) {
cout << "the number doesn't consist only of the digits 0..4."
}
cout << "0s: " << D(n, 0) << "\n";
cout << "1s: " << D(n, 1) << "\n";
cout << "2s: " << D(n, 2) << "\n";
cout << "3s: " << D(n, 3) << "\n";
cout << "4s: " << D(n, 4) << "\n";
You could also (or should also) use loops here to reduce the redundancy.

Counting individual word lengths in a string

#include <iostream>
#include <string>
#include <cctype>
size_t countwords(const char *);
using namespace std;
int main()
{
char a[] = "Four score and seven years ago";
float sum = 0.0;
char j[10];
string s;
int size = sizeof(a)/sizeof(char);
for(int i = 0; i < size; i++){
if(!isspace(a[i])){
s += a[i];
}
if(isspace(a[i]) and !isspace(a[i + 1])){
cout << s << " " << s.length() << endl;
sum += s.length();
s = "";
}
}
cout << countwords(a);
return 0;
}
size_t countwords( const char *s )
{
size_t count = 0;
while ( *s )
{
while ( isspace( *s )) ++s;
if ( *s ) ++count;
while ( isalnum( *s )) ++s;
}
return ( count );
}
In the main function, I am able to print each word and it's word length. such as four 4, score 5 etc. I am having trouble handling the last word "ago." I don't know how to account for that. Any help would be appreciated.
Output:
Four 4
score 5
and 3
seven 5
years 5
▼ 2
6
and yeah, don't know why that black triangle is in the output but this is the exact output.
The terminating NULL character is not considered whitespace, so your second if condition returns false when it encounters the end of the string.
Seems to me the statements within the for statement can be simplified to
if(!isspace(a[i]) && a[i]){
s += a[i];
} else {
cout << s << " " << s.length() << endl;
sum += s.length();
s = "";
}
Also, breaking the string apart at whitespace can be done easily using an istringstream
char a[] = "Four score and seven years ago";
std::istringstream ss(a);
std::string s;
while(ss >> s) {
std::cout << s << ' ' << s.length() << '\n';
}
The string you try to inspect is one character longer than you expect:
int size = sizeof(a)/sizeof(char);
This size includes the terminating null character. If I were to deal with the assignment I would either operator on char const* and use the C convention of checking against a terminating null character or I would convert the array into a std::string and deal with iterator and check against the end iterator. I also think that the logic you have to check against the end of a word assumes that words are separated by exactly one space.
Your countwords() function seems to deal with the C convention. Your main() function should check against a[i] being null before using !isspace(static_cast<unsigned char>(a[0])): the countwords() works because isspace(0) and isalnum(0) are false. However, just because 0 isn't a space it means it is part of a word. You should also consider the terminating null character a word separator, i.e., the condition to report the length of a word should be
if(!a[i] || isspace(static_cast<unsigned char>(a[i])))
std::string word;
std::istringstream str(a);
while (str >> word) {
sum += str.length();
std::cout << word << ' ' << word.length << '\n';
}

Sequence of numbers until the input is 0 (zero)

I've no idea how to make the following.
I would like to enter a sequence of numbers and when I enter 0, to stop the cin (like I clicked Enter) and automatically cout only those numbers who (number%2==0) on another line. Is it posible to be done with function?
I hope you understood me :)
For example I'm entering 123456785435430 (I've entered '0', so the cin stop immideately and cout me 2 4 6 8 4 4 3)
Here is a simple version that works with a given delimiter,
#include <iostream>
using namespace std;
string input_until_delimiter (char delimiter)
{
string buffer = new string ();
char c = delimiter;
while ((c = get()) != delimiter) buffer += c;
return buffer;
}
You'll need something along the lines of this:
char x;
std::vector<int> evens;
do {
std::cin.get(x);
int v = x - '0'; // int value
if (v % 2 == 0 and v != 0)
evens.push_back(v);
} while (x != '0');
for (std::vector<int>::iterator it = evens.begin(); it != evens.end(); ++it)
std::cout << (*it) << " ";
Here's the version for your exercise:
#include <vector>
#include <iostream>
void brc() {
int x;
std::cin >> x;
if (x == 0) return;
if (x % 2 == 0)
std::cout << x << " ";
brc();
}
int main() {
brc();
return 0;
}