Save names or strings - c++

Using CPP on visual studios 2019. If needing more info just comment.
The compiler would ask for a name/string, you would enter it then
it would ask again.
If you were to type the same thing twice, the compiler would say "you typed it twice".
The problem is that I don't know how I should go about saving uppercase and lowercase
words because if you were to just type in the same word with a capital the compiler
would think its a brand new word.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string word[100];
int x = 0;
while (1)
{
cout << "Enter word.";
cin >> word[x];
x++;
// I don't know how to check if words typed in are the same.
// Thats the question.
}
}

If you are considering two strings entered in different case as the same string, then you could store the inputted string by transforming it into lower/upper case and then check with the previous value and currently inputted value every time.
Considering you read into string s, you could store the string into lower/upper case by using std::transform() function.
std::transform(s.begin(), s.end(), s.begin(), ::toupper);
Remember to convert the read input into lower/upper case as well before checking both the strings. I gave you the basic idea of how your program would work. Now, write the code yourself.

You have to use std::transform with std::toupper.
Steps:
input word, save in std::string
input next word
check words: call
std::transform(s.begin(), s.end(), other.begin(), ::toupper);
for both words and if they are the same print first word

You can convert all of the entered word characters into a single case (lower case/upper case), then check if the result is present in the entered words set.
A complete example:
#include <algorithm>
#include <cctype>
#include <iostream>
#include <set>
#include <string>
int main() {
std::set<std::string> words{};
while (true) {
std::string word{};
std::cout << "Enter a word: " << std::flush;
std::cin >> word;
// Convert word to lower case by adding 32 to each upper case
// character
for (auto &c : word) {
c |= 0x20;
}
if (words.count(word)) {
std::cerr << "You typed `" << word << "' twice!" << std::endl;
continue;
}
words.emplace(std::move(word));
}
return 0;
}

Related

Extract all numbers from stringstream

I want to read string and extract all numbers.
Input: 5a3 1f a0aaaa f1fg3
Output: 53 1 0 13
I tried this code:
string s;
getline(cin, s);
stringstream str_strm(s);
int found;
string temp;
while (!str_strm.eof()) {
str_strm >> temp;
if (stringstream(temp) >> found)
{
cout << found << endl;
}
}
but when found 5 (from example)after that automatically start to check the other string. How can I extract all numbers?
Here's a possible solution - while loop is used to separate strings with whitespaces, after that digits are extracted from the sub-strings.
int main()
{
stringstream ss("5a3 1f a0aaaa f1fg3");
string str;
while (getline(ss, str, ' ') ){
str.erase(std::remove_if(str.begin(), str.end(), [](unsigned char c) { return !std::isdigit(c); }), str.end());
cout << str << " ";
}
}
You could read each space separated word, and then remove the non-digits, like this
std::string word;
while (std::cin >> word)
{
word.erase(std::remove_if(word.begin(), word.end(),
[](unsigned char c) { return not std::isdigit(c); }),
word.end());
std::cout << word << " ";
}
For the input of 5a3 1f a0aaaa f1fg3, it prints 53 1 0 13.
The admittedly odd way of removing elements of a range, is a common idiom.
You could even avoid the loop entirely, if you have the input on a single line
std::string word;
std::getline(std::cin, word);
word.erase(std::remove_if(word.begin(), word.end(),
[](unsigned char c) { return not std::isdigit(c)
and not std::isspace(c); }),
word.end());
std::cout << word;
Please see here the ultra simple example. (There is an even simpler solution at the bottom of this post)
It is using modern C++ elements and algorithms. And has only a few lines of code.
#include <iostream>
#include <string>
#include <regex>
#include <iterator>
#include <algorithm>
#include <sstream>
int main() {
// Read a string from the console
if (std::string line{}; std::getline(std::cin, line)) {
// Put the complete line into a std::istringstream
std::istringstream iss{line};
// Print result
std::transform(std::istream_iterator<std::string>(iss), {}, std::ostream_iterator<std::string>(std::cout, " "),
[](const std::string& s) { return std::regex_replace(s, std::regex{ R"([^\d])" }, ""); });
}
return 0;
}
So, what's going on here. Let us look at it statement by statement. So, first:
if (std::string line{}; std::getline(std::cin, line)) {
This is a if-statement with initializer. If you look up if in the C++ reference, here, then you can see, that we can now have an additional initialization statement as the first part in the if. And why are we using that? Because it is an additional measure for scoping. The variable "line" is only used within the scope of the if statement. It is not needed outside the if. From the functionality point of view, it is the same as writing:
std::string line{};
if (std::getline(std::cin, line)) {
But then, "line" would be also visible outside of the if statement. And, because we want to prevent the pollution of outer namespace, we select this method.
Next is std::getline. This will read a complete line from the input stream, so, from the console (std::cin)and put it into the string. The std::getline returns a reference to the stream. The stream has an overloaded bool operator, that returns, if there was a failure (or end of file) or not. So, the if statement checks, if the input operation works. By the way. All IO-opereations should be checked, if they work or fail.
Good, now we have the complete line of the user input in our variable "line".
With
std::istringstream iss{line};
we put the string into an std::istringstream. We do this, because we want to make use of the C++ "iostream" library. The std::istringstream behaves as any other stream, for example std::cin and you can extract values from it that are separated by a white space. Like in std::cin >> v1 >> v2. The disadvantage for such an approach is, that you need to know the number of values in advance or use a dynamic growing container and a loop.
And this brings ud to our next construct that I want to explain. You may have heard about "iterators". Iterators are like pointers and can point to a range of elements. If you have a std::vector or any other container, then you can iterate with the begin() and end() iterator over all elements in the std::vector without knowing, how many elements are in the std::vector, without knowing how many elements it contains.
And for input streams, we have something similar: The std::istream_iterator. This iterator will iterate over the elements in the std::sitringstream and returns the type of variable given in its template parameter, by repeatedly calling the extractor operator >>. Here, in our case, a std::string. You may know ask: Until when? Where is the end. If you look in the description of the constructor number 1 of the std::istream_operator then you will see, that the default constructor Constructs the end-of-stream iterator. and the default construct can be generated by using the empty braced {} initializer. So {} is the end iterator.
If we want to read all std::strings from the std::istringstream, then we read between
std::istream_iterator<std::string>(iss) and {}. So every string that is in the std::istringstream.
Good, next, there is a similar thing for output, the std::ostream_iterator. This will call the inserter operator "<<" for all elements in a given range. And, we can can specify, to which stream it should send the data, here std::cout and additionally a separator-string, which will be appended to the outputted value.
OK, next: std::transform. As it names says, it will transform the elements in a range of elements, between a begin() and end() iterator, to a other range. So, it will transform the elements as shown above from the std::istringstream and send them to the std::ostream iterator. So, we read the source value, transform it, then write it.
But, how to transform. For the transformation, we give a simple lambda function, which calls the std::regex_replace function. This is a standard function, to replace parts of a string with other string data. And, the what that will be replaced is specified by a std::regex. This is a special pattern that is defined in some kind of meta language and matches specified parts of a string. in our case we use [^\d] which means, not a digit. You can test regexes here. You can also lean about them here.
And now, all together, explains the above solution.
All this can be further optimized to 2 statements:
#include <iostream>
#include <string>
#include <regex>
int main() {
// Read a string from the console
if (std::string line{}; std::getline(std::cin, line)) {
// Remove unnecessary characters
std::cout << std::regex_replace(line, std::regex{ R"([^\d ])" }, "") << "\n";
}
return 0;
}
I cannot think of a more simpler solution.
In case of questions, please ask.
You can use get from istream to get each character, including whitespace, and then isdigit to check for a digit character...
#include <iostream>
#include <cctype>
int main()
{
char ch;
std::cin.get(ch);
while (!std::cin.eof())
{
if (isdigit(ch) || ch == ' ' || ch == '\n')
{
std::cout << ch;
}
std::cin.get(ch);
}
return 0;
}
However, you can avoid using std::cin.eof() for your expression for your While loop as follows...
#include <iostream>
#include <cctype>
int main()
{
char ch;
while (std::cin.get(ch))
{
if (isdigit(ch) || ch == ' ' || ch == '\n')
{
std::cout << ch;
}
}
return 0;
}
Regular expression pattern matching can be used to find all the digits in the input string.
Here is an example program to find the digits:
// C++ program to find all digits in a string
#include <bits/stdc++.h>
using namespace std;
int main() {
string inputString;
cout << "Enter the input string: ";
getline(cin, inputString);
cout << "Digits found: ";
// Define the regular expression matcher and pattern
smatch matcher;
regex pattern("[[:digit:]]");
while (regex_search(inputString, matcher, pattern)) {
// Show the match
cout << matcher.str(0);
// Continue searching the rest of the string
inputString = matcher.suffix().str();
}
return 0;
}
Output:
Enter the input string: sdfh354 eutyt;ljkn756897490uiotureu 587689jkgf 90
Digits found: 35475689749058768990
Here is another approach of finding the numbers in the string, without using the regular expression pattern matching:
#include <iostream>
#include <cctype>
#include <bits/stdc++.h>
using namespace std;
int main() {
string rawInput;
cout <<"Enter input string: ";
getline(cin, rawInput);
// Get all words from the input string
stringstream allWords(rawInput);
// Find and print digits in each word
string word;
while(allWords >> word) {
for(int i = 0; word[i]; i++) {
// Print only the numbers in the word
if(isdigit(word[i])) {
cout<<word[i];
}
}
cout<<" ";
}
cout<<"\n";
return 0;
}
Output:
Enter input string: ghjg45 jsdfj 897897 343yut45 90
45 897897 34345 90
How can I extract all numbers?
When you KNOW that the input numbers are all hex values ... (and how many)
stringstream ss ("5a3 1f a0aaaa f1fg3");
for (int i=0; i<4; ++i)
{
int k;
ss >> hex >> k;
cout << k << endl;
}
with output
1443
31
10529450
3871

How to store a single word in a vector ? (c++)

Idea:
I'm trying to create a program that searches for user-entered-word in a .txt file. Size of the word is not given. I want to find a way to dynamically store user's word to be able to compare it to the other words from file.
The entire program is huge so i only attach a part related to my queasions.
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <string>
void vectorfill(vector<char>& newword) //filling char vector
{
char input;
scanf_s("%c", &input);
while (input != -1)
{
newword.push_back(input);
scanf_s("%c", &input);
}
}
int main (void)
{
vector<char> word;
printf("Enter a word: (-1 to finish)");
vectorfill(word);
}
Questions:
1) Is char vector a best idea in this case ?
2) (In case we're good with char vector)How to make compiller understand that user finished writing their word? Can we ask him to put (-1) at the end? Is there a better way to mark the end of the input?
1> No. Use a std::string
2> Yes. Use whitespace.
Example:
#include <iostream>
#include <string>
int main ()
{
std::string word;
std::cout << "Enter a word" << std::endl;
std::cin >> word;
// do something with word. For example,
std::cout << "You entered" << word << '\n';
}
As soon as the user types in at least one number, letter, or other non-whitespace character followed by a whitespace character a word will have been captured in word. If you have special requirements like this word can only contain letters (no numbers, bells, ASCII art characters, etc...) a simple loop with isalpha can sort that out in a few lines of code, but not as few as std::find_if and isalpha.
If the searching content is in txt file. Using std::vector<std::string> may be better. You can use the split char to split the words.
If the content is from user's keyboard input. you can also use std::string to store every word typed, and store it in std::vector<std::string>. Just like this:
std::string s;
std::vector<std::string> vec;
std::cout << "Please enter somestring" << std::endl;
while (cin >> s)
{
vec.push_back(s);
cout << "You have entered : " << s << endl;
}

Why string receives numbers in this while loop?

A string is a variable-length sequence of characters. Why does it receive anything and prints it out?
#include <iostream>
#include <string>
using namespace std;
int main (){
string word;
while (cin >> word){
cout << word << endl;
}
return 0;
}
In this program, we read into a string, not an int. How can I fall out of this while loop i.e hit an invalid input?
Reading into a string will not fail, all input is valid. You may add any validation you like once the string is read.
Your question is a little vague, but if you're asking how to end the loop you can do it with an end-of-file. On Linux this you can generate one from the console with Control-D, and on Windows with Control-Z plus Enter.
Because you are taking the input in a string and string is a sequence of characters .so it takes anything you input from the keyboard either it is number or alphabet or any special character .
How can I check for invalid input?
If you could define what you consider to be "invalid input" you can filter for it in one of the std::string helper methods. In your example you eluded to numbers not being strings... so if you want to do something with pure numbers...
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main (){
string word;
while (cin >> word){
bool isNumber = (word.find_first_not_of("0123456789") == std::string::npos);
if (isNumber){
cout << "it's a number! " << word << endl;
}else{
cout << word << endl;
}
}
return 0;
}

How to use cin with unknown input types?

I have a C++ program which needs to take user input. The user input will either be two ints (for example: 1 3) or it will be a char (for example: s).
I know I can get the twos ints like this:
cin >> x >> y;
But how do I go about getting the value of the cin if a char is input instead? I know cin.fail() will be called but when I call cin.get(), it does not retrieve the character that was input.
Thanks for the help!
Use std::getline to read the input into a string, then use std::istringstream to parse the values out.
You can do this in c++11. This solution is robust, will ignore spaces.
This is compiled with clang++-libc++ in ubuntu 13.10. Note that gcc doesn't have a full regex implementation yet, but you could use Boost.Regex as an alternative.
EDIT: Added negative numbers handling.
#include <regex>
#include <iostream>
#include <string>
#include <utility>
using namespace std;
int main() {
regex pattern(R"(\s*(-?\d+)\s+(-?\d+)\s*|\s*([[:alpha:]])\s*)");
string input;
smatch match;
char a_char;
pair<int, int> two_ints;
while (getline(cin, input)) {
if (regex_match(input, match, pattern)) {
if (match[3].matched) {
cout << match[3] << endl;
a_char = match[3].str()[0];
}
else {
cout << match[1] << " " << match[2] << endl;
two_ints = {stoi(match[1]), stoi(match[2])};
}
}
}
}

How to use nested getline() function to get ride of special characters and punctuation in a string?

I am using getline() function to get ride of special characters and punctuation in a sentence, so that when i display the words contained in the sentence, it does not have any other character beside a-z (or A-Z). The problem is that it gets long, and I don't think it is really efficient. I would like to know if I can do it in a efficient way. I am using Dev-C++, the code below is in C++. Thanks for your help.
#include <string>
#include <iostream>
#include <ctype.h>
#include <sstream>
using namespace std;
int main()
{
int i=0;
char y;
string prose, word, word1, word2;
cout << "Enter a sentence: ";
getline(cin, prose);
string mot;
stringstream ss(prose);
y=prose[i++];
if (y=' ') // if character space is encoutered...
cout<<endl << "list of words in the prose " << endl;
cout << "---------------------------"<<endl;
while(getline(ss, word, y)) //remove the space...
{
stringstream ss1(word);
while(getline(ss1, word1, ',')) //remove the comma...
{
stringstream ss2(word1); //remove the period
while(getline(ss2, word2, '.'))
cout<< word2 <<endl; //and display just the word without space, comma or period.
}
}
cout<<'\n';
system ("Pause");
return 0;
}
#############################output
Enter a sentence: What? When i say: "Nicole, bring me my slippers, and give me m
y night-cap," is that prose?
list of words in the prose
What?
When
i
say:
"Nicole
bring
me
my
slippers
and
give
me
my
night-cap
"
is
that
prose?
Press any key to continue . . .
Use std::remove_if():
std::string s(":;[{abcd 8239234");
s.erase(std::remove_if(s.begin(),
s.end(),
[](const char c) { return !isalpha(c); }),
s.end());
If you do not have a C++11 compiler, define a predicate instead of using a lambda (online demo http://ideone.com/NvhKq).