Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to count the no.of times SUVO and SUVOJIT appears in the long string.

Input=>
5
SUVOJITSUVOSUVOJITUCSUVOJITSUVOVXSUVOJITSUVOGMSUVODMMVDSUVOJIT
AXRSUVOJITHSUVOJITHSUVOJJSUVOJITSUVOJIT
SUVOJITPRQSUVOJIT
SUVOJITTXGSUVOUNSUVOJIT
SUVOJITSUVOSUVOJITXGSUVOSUVOQSUVOJITKDSALASUVOQESUVOHSSUVODFSUVOJITWSUVOUSUVOJITGJEM


In the below program when I take getline for taking string input the output it is generating consist of SUVO=0,SUVOJIT=0 in the first Line always .While the cin>> is not producing this kind of behaviour .Why?

C++
#include <iostream>
#include <string>

using namespace std;

int main(){
  int n;
  cin>>n;
  string s;
  for(int i=0;i<n;i++){ 
    cin>>s; //getline(cin,s)
    int s1=0,s2=0;
    for(int j=0;j<s.length();j++){
      if(s[j] == 'S'){
        if(s.substr(j,7).compare("SUVOJIT") == 0)
          s1++;
        else if(s.substr(j,4).compare("SUVO") == 0)
                s2++;
      }
      //
    }
       //
                
      cout<<"SUVO="<<s2<<", SUVOJIT="<<s1<<endl;
  }
                
         return 0;
                }


What I have tried:

Input=>
5
SUVOJITSUVOSUVOJITUCSUVOJITSUVOVXSUVOJITSUVOGMSUVODMMVDSUVOJIT
AXRSUVOJITHSUVOJITHSUVOJJSUVOJITSUVOJIT
SUVOJITPRQSUVOJIT
SUVOJITTXGSUVOUNSUVOJIT
SUVOJITSUVOSUVOJITXGSUVOSUVOQSUVOJITKDSALASUVOQESUVOHSSUVODFSUVOJITWSUVOUSUVOJITGJEM
Posted
Updated 21-Aug-18 6:27am
v3

That is because getline (string) - C++ Reference[^] and the istream::operator>> - C++ Reference[^] behave differently.

getline() reads until the delimiting character (newline \n by default) is read and discards that character.

The istream >> operator with strings skips leading white spaces (like space, tab, newline) and reads then until a white space character is encountered. That white space character is not read but left in the input buffer.

You are calling
cin>>n;
initially. That will read the entered number but will let the newline character in the input buffer. When calling getline() afterwards, that will read the newline as first character and stop. That is: you got an empty string. When using the istream >> operator instead, it will skip the newline character and read the content of the next line.

Due to this different behaviour it is not recommended to mix getline() and >> operator calls for the same stream.
 
Share this answer
 
v2
You should pay attention that "SUVO" is a part of "SUVOJIT" and so "SUVOJIT" is also an occurance of "SUVO" and CANT occur without"SUVO".

C++
if(s[j] == 'S'){
    if(s.substr(j,4).compare("SUVO") == 0) {
      s1++;
    if(s.substr(j,7).compare("SUVOJIT") == 0) //removed else
      s2++;
    }
}
Remark: Make some output for the user before input and I really wouldnt use s1 or s2 for a int count => count1 and count2.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900