Bug in if statement [duplicate] - c++

This question already has answers here:
C++ if else if not working properly [duplicate]
(5 answers)
Closed 5 years ago.
No matter what string I add to s[i], I I still get a "yes" as an output while if I remove ||(OR) it works perfectly.
for(int i=0; i<T; i++)
{
if(s[i]=="ccc"||"ccs")
{
cout<<"yes"<<endl;
}
else
{
cout<<"no"<<endl;
}

This is how it's written:
if(s[i] == "ccc" || s[i] == "ccs")

Change your if condition to:
if(s[i] == "ccc" || s[i] == "ccs")

Related

Getting an error while reversing vowels in a 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 3 years ago.
Improve this question
I know there are similar questions about this topic but I wanted to know error in my approach.
I'm writing a code to reverse vowels in a string. I first took all the vowels of a string into a vector and then I looped the string from backwards by replacing the vowels but I keep getting an error.
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
string reverseVowels(string s)
{
vector<char>v;
vector<char>v2;
char c;
for (int i = 0; i < s.size(); i++)
{
//taking all the vowels of the string into a vector
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u')
{
v.push_back(s[i]);
}
}
//reversing the vowels of the string
for (int i = s.size() - 1; i >= 0; i--)
{
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u')
{
s[i] = v[i]; //Getting an error here
}
}
return s;
}
int main()
{
//Required output is "holle"
string s = "hello";
string p = reverseVowels(s);
cout << p << endl;
return 0;
}
You need another index for the reversed vowels:
for (int vowelIdx = 0, i = s.size() - 1; i >= 0; i--)
{
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u')
{
s[i] = v[vowelIdx++]; //Getting an error here
}
}
edit
Regarding your comment below; let's examine the code for the word hello. The first loop runs through all the vowels in hello and fills the vector v as eo. It has two vowels with e as the first and o as the second/last element.
Your second loop reverse traverses the word hello, and encounters the first vowel o. You need to reset this to e which is the first element of the vector v. This is where your code get an error. The value of i is 4 now, hence you are trying to use the 4.th index of vector v which has only two elements. You have to set s[4] = v[0] and s[1] = v[1]. However, your code tries to set s[4] = v[4] (see the error?) and s[1] = v[1].
Hope it's clear now, if not try it on paper with a longer word. I'm sure you will see the point.

How is the value of b unchanged? [duplicate]

This question already has answers here:
Is short-circuiting logical operators mandated? And evaluation order?
(7 answers)
Closed 6 years ago.
How is the value of b unchanged?
#include <iostream>
int main()
{
int a = 5, b = 10;
if (++a || ++b)
std::cout << a << b;
system("PAUSE");
return 0;
}
The output is 610. But how?
here's how the 'if' statement works:
if(condition1 || condition2 || condition 3){
//do this
}
now if condition1 is true (which in your code, it is since a!=0), the execution straightaway moves inside the block without checking 2 and 3.
If you wish to increment b as well, try && in place of ||

C++ : What is the usage of int('0') in this code? [duplicate]

This question already has answers here:
How to convert a single char into an int [duplicate]
(11 answers)
Closed 7 years ago.
This code Find the sum of all digits that occur in a string.
Example
sumUpNumbers("2 apples, 12 oranges") = 5 //2+1+2
Can anyone explain the need for use int('0') in this code!?
int sumUpDigits(std::string inputString) {
int answer = 0;
for (int i = 0; i < inputString.size(); i++) {
if ('1' <= inputString[i] && inputString[i] <= '9') {
answer += int(inputString[i]) - int('0');
}
}
return answer;
}
It converts char into ASCII code to make number out of string
int('9') - int('0') = 9

have a string in c++, need help to split it [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 8 years ago.
Improve this question
#include<iostream>
#include<string>
#include<ctype.h>
#include<vector>
using namespace std;
int main(){
vector<string>temp;
int m = 0;
string strOperand = "10 7 5.0 * -";
string strCurData = "";
for (int i = 0; i < strOperand.size(); i++)
{
if (isdigit(strOperand[i]) || strOperand[i] == '.')
{
strCurData.push_back(strOperand[i]);
}
else
if (!isdigit(strOperand[i]) && strOperand[i] != '.'){
if (strOperand[i] == ' ')
{
temp[m] = strCurData;
strCurData = "";
m++;
}
else
if (strOperand[i] == '+' || strOperand[i] == '-' || strOperand[i] == '*' || strOperand[i] == '/' || strOperand[i] == '%' || strOperand[i] == '^')
{
temp[m] = strOperand[i];
m++;
}
else
if (strOperand[i]=='\0')
{
break;
}
}
}
cout << m;
}
i want to split the string to a vector, but it turns to be a error warning that vector subscript is out of range, i am really worry about this, can someone please help me, thanks!
temp is empty, yet you access its nonexistent elements by index. The behavior you describe is as expected.

I'm having trouble with finding unique words, how to improve code? [duplicate]

This question already has an answer here:
How to improve my code to correctly count unique lines?
(1 answer)
Closed 9 years ago.
I need to count the number of unique words and with my code below, it doesn't seem to be counting correctly. I am not sure what else I can do to make it work and would really appreciate any suggestions.
#include <iostream>
#include <string>
#include <set>
using std::string;
using std::set;
unsigned long countUWords(const string& s)
{
set<string> uw;
string word = "";
for(size_t i = 0; i < s.size(); i++){
bool words = (s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z');
if(words){
word += s[i];
}
else if(!words && word != ""){
uw.insert(word);
word = "";
}
}
if (word != "")
uw.insert(word);
return uw.size();
}
int main ()
{
string s;
unsigned long UWords = 0;
while(getline(cin, s)){
UWords += countUWords(s);
}
cout << UWords << endl;
return 0;
}
At the end of the for loop you need to check if word is empty. If not you need to push the content of word inside the set.
After the end bracket of the for loop, add:
if (word != "")
uw.insert(word);
As you can see it works just fine after that edit.