How to accept case-insensitive inputs? [duplicate] - c++

This question already has answers here:
Case-insensitive string comparison in C++ [closed]
(30 answers)
Closed 5 years ago.
How do you accept case-insensitive and allow embedded blanks in a user input? So the user can enter “hong konG” and get a correct match to the input.
I only have the input[0] = toupper(input[0]); which only accepts if the case sensitive is at the beginning of the word.
while(true){
cout << "Enter a city by name: "<< " ";
std::getline (std::cin,input);
if (input == "quit")
{
break;
}
input[0] = toupper (input[0]);
//....how do I loop to find all letter's in the input string variable?
}

You can use a loop to convert the entire string to upper case one character at a time, but a better solution is to use C++ standard library's transform function for that:
std::string hk = "hong konG";
std::transform(hk.begin(), hk.end(), hk.begin(), ::toupper);
This would apply ::toupper to all characters of your string, resulting in a string that reads "HONG KONG".
Demo on ideone.

for (auto& c : str)
c = std::toupper(c)

You can convert the whole string to upper-case like this
for (size_t i = 0; i < input.size(); ++i)
input[i] = toupper (input[i]);
The other suggestion to use std::transform is also a perfectly good solution.

Related

Cut string in words with ------- as delimiter [duplicate]

This question already has answers here:
Parse (split) a string in C++ using string delimiter (standard C++)
(33 answers)
Closed 1 year ago.
Hello i have the following sentence which is in a string and in one Line but very long:
l-s-s---s-l---s-s-s-------s-l---l---l-s-s-s-s-l---l-l-s-s---s---s-s---l-s-l-s---s-s-s-s---s---l-s-------l-s-l-l-s---s-l-l-s-l-s---l-s-l-l-s-l-------
(Its in Morse Code) The --- (3x-) seperates the letters and ------- (7x-) seperates the word.
How can i cut the very long code in words.
I've tried the following:
size_t posWordNext{};
size_t posWordPre{};
while (true) {
posWordNext += code.find("-------");
if (posWordNext >= code.size()) {
break;
}
cout << code.substr(posWordPre, posWordNext) << endl;
posWordPre = posWordNext;
}
This is the output:
l-s-s---s-l---s-s-s
-------s-l---l---l-s-s-s-s-l---l-l-s-s
s-s-s-s-l---l-l-s-s---s---s-s---l-s-l-s---s-s-s-s---s---l
---s---s-s---l-s-l-s---s-s-s-s---s---l-s-------l-s-l-l-s---s-l-l-s-l-s---l-s
Every time you call code.find(), you are searching from the beginning of the string again. You are not modifying code on each iteration, so find() will return the same offset each time. You should be passing posWordPre to the 2nd parameter of find() as a starting offset to begin searching from.
Also, when you are calling code.substr(), you are treating the 2nd parameter as an offset to stop at, but that parameter actually expects a character count instead of an offset.
Because of these mistakes, you are chopping up the code string at the wrong offsets.
Try something more like this instead:
size_t posWordPre = 0, posWordNext;
while ((posWordNext = code.find("-------", posWordPre)) != string::npos) {
cout << code.substr(posWordPre, posWordNext - posWordPre) << endl;
posWordPre = posWordNext + 7;
}
if (posWordPre < code.size())
cout << code.substr(posWordPre) << endl;
Online Demo

How to find the lenght of the word in C++? [duplicate]

This question already has answers here:
std::string length() and size() member functions
(4 answers)
Closed 2 years ago.
We have some word, that we should identify the length of. How can I do that?
Example INPUT: "hello" - without quotes;
Example OUTPUT: 5
If input is contained in a std::string you can find the length as stated by Ravi.
If it's a C string you find the length with
int len = strlen(INPUT);
In C/C++ upper case is normally used for constants so it's better to name the string input, not INPUT.
string str;
cin>>str;
//use this
cout<<str.length();
//or use this
cout<<str.size();
both of them will work fine.
There is one function to find length in C++.
You can try by using:
input.length()
Also remember, you need to include:
#include <iostream>
using namespace std;
Refer this document :
https://www.w3schools.com/cpp/cpp_strings_length.asp
You can use input.length() or input.size().
Or you can use this simple loop.
for (i = 0; str[i]; i++)
;
cout << i << endl;

How can I seperate a string into individual characters in c++? [duplicate]

This question already has answers here:
For every character in string
(10 answers)
Closed 8 years ago.
As the title says, how can I seperate a string into individual characters in c++? For example, if the string is "cat" how can I separate that into the characters c,a, and t?
Thanks
By using the operator[]. e.g.
std::string cat{"cat"};
if(cat[0] == 'c')
//stuff
If you're using std::string you can simply use .c_str( ) that will give you an array of the chars.
In c++11 you could also do:
for( auto c : a )
{
cout << c << '\n';
}
http://ideone.com/UAyxTo
If you want to store them in a vector:
string str("cat");
vector<char> chars(str.begin(), str.end());
for (char c : chars)
cout << c << endl;

how to split a string on comma in C++ [duplicate]

This question already has answers here:
How do I tokenize a string in C++?
(37 answers)
Closed 10 years ago.
I have this code currently in which the user would enter a number for how many numbers they want in the array. followed by '12345' however about a second after writing it i realized this would only work if they entered number 0-9 anything in double figures or more wouldnt work.
int numberOfValues;
cout << "Please enter the amount of integers you want in the array" << endl;
cin >> numberOfValues;
int valuesArray[numberOfValues];
string valuesString;
cout << "Please Enter " << numberOfValues << " numbers" << endl;
cin>>valuesString;
for(int i = 0; i < numberOfValues; i++)
{
valuesArray[i] = valuesString[i];
}
return valuesArray;
im thinking that the best way to do this would be for the user to enter numbers separated by a comma and to split them afterwards (iv done this same little porgram in java and trying to change it to C++ for my own personal learning) like in java i used string.split(",") i was wondering if there is anything similar in c++??
The simplest way I can think of would be to avoid reading to an intermediate string and let cin do the work for you:
std::vector<int> valuesArray;
int i = 0;
do {
cin >> i;
valuesArray.push_back(i);
} while (valuesArray.size() < numberOfValues && cin.get() == ',');
/* edit: You may need to consume a '\n', if you expect one, too: */
do {
i = cin.get();
} while (i != '\n');
Use strtok. Documentation and example can be found Here
use combination of string::substr() and string::find().
Find the next comma charater and then find the substring from current location to next command character
It is not standard C++ string, but still, Qt's QString class provides a ready-to-use method QString::split(...) with support for stuff like regular expressions, options for split behavior, case sensitivity and whatnot...
I wrote a tokenizer time ago, hope it works for you:
std::vector<std::string> tokenize(const std::string &_line,const char *_delimeter)
{
std::vector<std::string> Tokens;
if(_line.empty()) return Tokens;
std::string str;
BOOST_FOREACH(char c,_line){
if(c==*_delimeter)
{
Tokens.push_back(str);
str.clear();
continue;
}
str += c;
}
if(!str.empty())
Tokens.push_back(str);
return Tokens;
}
it is not efficient, but works for testing purpose.

In C++ How can I tell the difference between integers and characters?

I am currently learning C++ and I have been asked to make a program which will calculate the interest that would be paid on a deposit of a given size. One of the requirements is that we display an error message when non-integer data is entered.
I however cannot work out how to detect if non-integer data has been entered. If anyone could provide an example of how this problem is solved it would be much appreciated!
You don't have to check yourself. The expression (std::cin >> YourInteger) evaluates to a bool, whcih is true if and only if YourInteger was succesfully read. This leads to the idiom
int YourInteger;
if (std::cin >> YourInteger) {
std::cout << YourInteger << std::endl;
} else {
std::cout << "Not an integer\n";
}
this should be a clear enough starting point.
char* GetInt(char* str, int& n)
{
n = 0;
// skip over all non-digit characters
while(*str && !isdigit(*str) )
++str;
// convert all digits to an integer
while( *str && isdigit(*str) )
{
n = (n * 10) + *str - '0';
++str;
}
return str;
}
You need to find out if the input value contains non numeric characters. That is, anything other than 0-9.
You have to first take input as string and then verify if every digit is indeed numeric.
You can iterate the string and test if each character is a valid digit using the built in function isdigit() defined in <cctype>. You might also want to allow for a single comma if you're working with decimal numbers.