This code is giving output YES just after entering the value of test case.
Code: for alphanumeric pallindrome
int main() {
int t;
cin>>t;
while(t--){
string s;
int count = 0,size = 0;
getline(cin,s);
cout<<s<<endl;
s.erase(remove_if(s.begin(),s.end(),not1(ptr_fun((int(*)(int))isalnum))), s.end());
for(int i=0;i<=s.size()/2;i++){
size++;
if(tolower(s[i])==tolower(s[s.size()-i-1])){
count++;
}
else
break;
}
if (count==size)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}
Output I am getting is YES without giving any input of string
For Input:
2
I am :IronnorI Ma, i
Ab?/Ba
Your Output is:
YES
I am :IronnorI Ma, i
YES
This code is giving output YES just after entering the value of test
case. Output I am getting is YES without giving any input of string
Your problem is here:
/* code */
cin>>t; -----------> std::cin
while(t--)
{
string s;
int count = 0,size = 0;
getline(cin,s); ------------> std::getline()
/* remaining code */
Reading with something like std::cin leaves the newline in the input stream. When the flow of control reaches std::getline(), the newline will be discarded, but the input will cease immediately. This leads, std::getline()s attempt read a new line and skips the input.
FIX: When switching from white space delimited to newline delimited input, you want to clean all newlines from the input stream by doing a std::cin.ignore()
The fixed code should be: https://www.ideone.com/ucDa7i#stdin
#include <iostream>
#include <string>
#include <limits>
#include <algorithm>
int main()
{
int t;
std::cin >> t;
// fix
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
while(t--)
{
std::string s;
int count = 0,size = 0;
getline(std::cin,s);
/* remaining code */
}
Related
I am working on a programming exercise, "Count of camel case characters" using C++. The goal of the exercise is to count the number of upper-case letters in a given string (what the exercise calls "camel case").
So given the following two inputs:
ckjkUUYII
HKJT
I would expect to get the following counts respectively:
5
4
Based on the code I've include below, however, I am instead getting:
0
5
This is clearly incorrect, but I've having difficulty isolating the problem in my code. How can I reason through this problem, or debug my error?
#include <iostream>
#include <cstring>
using namespace std;
int main() {
int t;cin>>t;
while(t--)
{
int res=0;
string str;
getline(cin,str);
int len= str.length();
for(int i=0;i<len;i++)
{
int c=str[i];
if(isupper(c))
res=res+1;
}
cout<<res<<endl;
}
//return 0;
}
After entering integer value as t, in input buffer newline character is left. So first call of getline gives you empty string as a result. You have to do:
int t;
cin >> t;
cin.ignore();
while (t--) {
...
}
to consume newline character, then both calls of getline will return entered strings properly.
The main problem is that you are trying to convert a character into an integer in line 15. An integer can't be either uppercase or lowercase. hence it gives the wrong answer. Simply check isupper(s[i]) as it would give the correct answer.
Consider my code,
#include <bits/stdc++.h>
using namespace std ;
int main() {
int t ; cin >> t ;
while(t--) {
string s ; cin >> s ;
int cnt = 0 , ln = s.size() ;
for(int i = 0 ; i < ln ; i ++) {
if(isupper(s[i])) cnt ++ ;
}
cout << cnt << endl ;
}
return 0;
}
I need help with the following snippet:
Using string in the following loop terminates:
int main() {
string in;
while(1){
cin >> in;
if (in == "|")
break;
}
But using int in the following loop does not terminates:
int main() {
int in;
while(1){
cin >> in;
if (in == '|')
break;
else
cout<< in << "\n";
}
I want to terminate the last shown snippet. Is it possible to do using int in.
I've seen the post C++ Terminate loop using a char input to int but no solution.
While characters are represented in the computer as small integers, an int is not the same as a char. Not in C++.
When you read into an int variable the >> operator tries to parse the input as an integer, as a number and not as a character.
If you want to read a character then read a character:
char in;
std::cin >> in;
If you try to read an integer, and the input is not a number, then the input operator will fail. See e.g. this very simple example.
This is the approach I used
I needed to input integers to a vector as input using while loop indefinitely and terminate it using a character.
This approach takes the input as character and then convert this char into integer.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
char in;
std::vector<int> vec;
while(1){
cin>>in;
if(in == '|'){
break;
}
vec.push_back(in - '0');
}
for(int j=0; j<vec.size(); j++){
cout<<vec[j];}
return 0;
}
When I enter "1", for loop should run one time. but it just prints "0". I don't know why.
I think the problem is with "getline(cin, input)" but i dont know the problem.
Here is the code:
int main()
{
string input;
int t, output, occured_length, lenght, match;
char occured[26];
cin>>t;
for(int i=0; i<t; i++) //I am talking about this loop
{
occured_length = 0;
getline(cin, input); //This might be causing the problem
lenght = input.size();
for(int j=0; j<lenght; j++)
{
if(occured_length == 25)
{
cout<<"\n"<<occured_length+1;
break;
}
match = 0;
for(int k=0; k<occured_length; k++)
{
if(input[j] == occured[k])
{
match= 1;
break;
}
}
if(match == 0)
{
occured_length++;
occured[occured_length] = input[i];
}
}
cout<<"\n"<<occured_length;
}
return 0;
}
input is empty & length is 0
istream& getline (istream& is, string& str);
Gets line from input stream into string. It extracts characters from is and stores them into str until the the newline character, '\n' is found.
\n remains in the input stream, you need to another dummy input reading, otherwise you will get input = "" on next getline
cin>>t;
char c;
cin >> c;
The problem is that after the input with using operator >>
cin>>t;
the input buffer will contain new line character. And next statement with getline
getline(cin, input);
reads an empty string.
You have to remove the new line character from the input buffer usig method ignore. For example
#include <limits>
//...
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
The call of ignore should be before getline and after operator >>.
I am using getline and ignore but something is not working properly,
Below is the sample code which am not able to understand how it is working.
int main()
{
string str;
int t,length;
cin>>t; // t is the number of test cases
while(t--!=0)
{
cin.ignore();
getline(cin,str);
length=str.size();
cout<<"length="<<length;
}
}
Sample output:
2
hey hi
length 6
hey hi
length 5
Why is the length decreasing? Is this because of getline and ignore function? Any help would be appreciated.
The reason it is giving a different length is becaus your ignore() function ignores only one character. The first time round it ignores the return key you pressed after entering the number. But std::getline() deletes the return character for you. So the second time round ignore() deletes the first letter of your string making it "eh hi".
int main()
{
string str;
int t, length;
cin >> t; // does not remove the RETURN character
while(t-- != 0)
{
// first removed RETURN character after removes first letter
cin.ignore();
getline(cin, str);
length = str.size();
cout << "length = " << length;
}
}
Try using this instead:
int main()
{
string str;
int t, length;
cin >> t; // does not remove the RETURN character
while(t-- != 0)
{
// cin.ignore(); // dont do this here please
// cin >> ws skips all whitespace characters
// including the return character
getline(cin >> ws, str);
length = str.size();
cout << " length = " << length;
}
}
Alternatively (maybe better) you can move the ignore() function out of the loop to where t is really needed:
#include <limits>
int main()
{
string str;
int t, length;
cin >> t; // does not remove the RETURN character
// ignore as many characters as necessary including the return
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
while(t-- != 0)
{
// cin.ignore(); // not here
getline(cin, str);
length = str.size();
cout << " length = " << length;
}
}
The cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); looks complicated but it is the only way to guarantee that any spurious characters (like spaces) are removed. You can probably get away with just cin.ignore() for the exercise if you want.
Read up on std::istream::ignore()
cin.ignore() defaults to ignoring one character.
If you output your string each time, you'll see that in later cases the string is equal to "ey hi". The h is being dropped.
The value of the string held by cin drops its first character before being passed to getline.
Since you're using getline, you can simply remove the cin.ignore() from your loop and your program should work as intended.
However, you should also change your cin>>t; line. In this case, the ignore() is dropping the line return after the input value 2. A stringstream here allows for a getline(...) function, or alternatively you can use cin.ignore(str.max_size(), '\n');.
In the case of the stringstream, your code would become:
#include <sstream> // stringstream
#include <string> // std::string
#include <iostream> // cin
int main()
{
string str;
int t,length;
getline(cin, str);
std::stringstream stream;
stream << str;
if (!(stream >> t)) {
// Couldn't process to int
}
// cin>>t; // t is the number of test cases
// No longer need this line.
while(t--!=0)
{
// cin.ignore(); Drop this too
getline(cin,str);
length=str.size();
cout<<"length="<<length;
}
}
If you are not interested in whitespace,
then use getline(cin >> ws, str)
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include <string>
#include <algorithm>
using namespace std;
string A,B;
int main()
{
int t,l,i;
scanf("%d",&t);
while(t--)
{
std::string str;
std::getline(cin, str);
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
cout<<str;
/*for(i=0;i<s.length();i++)
{
s[i]=toupper(s[i]);
}*/
}
return 0;
}
i wrote this code to convert to lower case but when run on ideone with input
1
hola please!!!
it shows output no can u tell or correct it morever this will help me learn the use of std:lowercase function taking getline cin as input rather than considering it as array
The problem is definitely with the way you are reading in. scanf("%d",&t); doesn't consume the return character, so it is still there on the getline, which will get an empty sting. Change it to scanf("%d\r",&t).
If you simplify the code, the problem can be reduced to:
int main() {
int t;
scanf("%d",&t);
std::string str;
std::getline(cin, str);
std::cout<<str;
}
This is because scanf reads from the input until it reaches a character that doesn't match, and it leaves that character in the input buffer. You input is "1\nhola please!\n\0", so scanf reads the 1, and leaves the rest: "\nhola please!\n\0". Then, you tell it to read until the next \n is found, which is immediately, so it returns an empty string.
The solution is: after using std::cin >> or scanf or whatever, if the next thing you want to do is read the rest of the line, you want to ignore that leading newline character. The C++ way to do this is std::cin.ignore(0xFFFFFFFF,'\n');, which will ignore the rest of the line. Alternatively, use scanf("%d\n" &t); to get the scanf to read in the newline character.
Related, you never check the validity of any of your input:
int main() {
int num_lines=0; //variables on different lines and initialized
std::cin >> num_lines
if (!std::cin)
return 1;
std::string str;
for(int j=0; j<num_lines && std::getline(sd::cin, str); ++j) {
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
std::cout << str;
}
return 0;
}
Or simply use < boost/algorithm/string.hpp > header and function boost::to_lower().
int t = 0;
scanf("%d",&t);
std::cin.ignore();
while(t--) {
std::string str;
std::getline(cin, str);
boost::to_lower(str);
std::cout << str << std::endl;
}