This is supposed to return false if a character in the string isn't a letter or an apostrophe. Any idea why it doesn't work? And is there a better way that I can write it? I'm trying to write code like a C++ purist.
for (std::string::const_iterator it = S.begin(); it != S.end(); ++it)
if ((*it < 'a' || *it >'z') && (*it > 'A' || *it < 'Z') && (*it != ''''))
return false;
I see two mistakes:
'''' should be '\''.
*it > 'A' || *it < 'Z' should be *it < 'A' || *it > 'Z'.
Related
I'm trying to solve a problem on a competitive programming book where the output only appears after entering in the last input. I seem to have gotten the logic down but I'm still confuse as to how to do the input/output portion.
Here is the code:
#include <bits/stdc++.h>
int main()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::vector<int>soundex;
std::string word;
for(int i = 0; i < word.length(); i++)
{
if (word[i] == 'B'|| word[i] == 'F' || word[i] == 'P' || word[i] == 'V')
{
soundex.push_back(1);
}
if (word[i] == 'C' || word[i] == 'G' || word[i] == 'J' || word[i] == 'K' || word[i] == 'Q' || word[i] == 'S' || word[i] == 'X' || word[i] == 'Z')
{
soundex.push_back(2);
}
if (word[i] == 'D' || word[i] == 'T')
{
soundex.push_back(3);
}
if (word[i] == 'L')
{
soundex.push_back(4);
}
if (word[i] == 'M' || word[i] == 'N')
{
soundex.push_back(5);
}
if (word[i] == 'R')
{
soundex.push_back(6);
}
}
for (int j = 0; j < soundex.size(); j++)
{
if (soundex[j] == soundex[j+1])
{
soundex.erase(soundex.begin() + 1);
}
std::cout << soundex[j];
}
std::cout << "\n";
return 0;
}
It behaves like this:
Input:
KHAWN
Output:
25
Input:
PFISTER
Output:
1236
Input:
BOBBY
Output:
11
But I need it to behave like this, per the instructions of the problem:
Input:
KHAWN
PFISTER
BOBBY
Output:
25
1236
11
Use while(cin >> word){ ... your code ... } to read until EOF (End Of File) in case every line only contains a word (no spaces allowed). You can keep the output as it is.
I'm solving a little problem and I met an runtime error and I have no idea where is the source of that error. I'm taking a string word and converting it's uppercase consonants to the corresponding lowercase ones by building another string new_word. The error appears at the else if statement.
The error: C++ exception: std::out_of_range at memory location 0x006FF330.
Please help!
int main()
{
string word, new_word;
string::iterator i;
cin >> word;
for (i = word.begin(); i < word.end(); i++)
{
if (*i != 'A' && *i != 'a' && *i != 'O' && *i != 'o' && *i != 'Y' && *i != 'y' && *i != 'E' && *i != 'e' && *i != 'U' && *i != 'u' && *i != 'I' && *i != 'i')
{
if (*i > 'a' && *i <= 'z')
{
new_word.push_back('.');
new_word.push_back(*i);
}
else if (*i > 'A' && *i <= 'Z')
{
new_word.push_back(word.at(*i)+32);
}
}
}
cout << new_word;
return 0;
}
It seems you mean
new_word.push_back( *i +32);
instead of
new_word.push_back(word.at(*i)+32);
Pay attention to that instead of using magic numbers like 32 you could use the standard C function tolower declared in the header <cctype>. For example
#include <cctype>
//...
new_word.push_back( std::tolower( ( unsigned char )*i ) );
I have seen many good code to do this problem. I am new to coding. My question is where my logic went wrong. I think that problem is with second string str1. I din't initialize it. even when I am printing element by element withing if , it is working. but it is not working, when I am trying to print whole string str1.
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str = "Hello, have a good day", str1;
for (int i = 0, j =0; i < str.length(); ++i)
{
if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z'))
if (str[i] == 'I' || str[i] == 'i' || str[i] == 'U' || str[i] == 'u' || str[i] == 'O' || str[i] == 'o' ||
str[i] == 'A' || str[i] == 'a' || str[i] == 'E' || str[i] == 'e' )
{
str1[j] = str[i];
//std::cout << str1[j] ;
j++;
}
else
{
str1[j] = str[i];
j++;
}
}
cout << str1 <<'\n';
}
output is just blank.
The first thing to do is to write a function that determines whether a character is a consonant:
bool is_not_consonant(char ch) {
static char consonants[] = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ";
return std::find(std::begin(consonants), std::end(consonants), ch) == std::end(consonants);
}
Then use that function as a predicate to std::copy_if:
std::string result;
std::string input = whatever;
std::copy_if(std::begin(input), std::end(input),
std::back_inserter(result),
is_not_consonant);
Explanation
The problem is that you don't need the else condition. All you need to do is check for a vowel, and print if found which is rightly covered in your if condition.
Code
Try this:
#include<string>
using namespace std;
int main()
{
string str = "Hello, have a good day", str1;
for (int i = 0; i < str.length(); ++i)
{
if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z'))
if (str[i] == 'I' || str[i] == 'i' || str[i] == 'U' || str[i] == 'u' || str[i] == 'O' || str[i] == 'o' || str[i] == 'A' || str[i] == 'a' || str[i] == 'E' || str[i] == 'e' )
{
str1 += str[i];
}
}
cout << str1 <<'\n';
}
3.Develop an algorithm that keeps asking the user to enter a valid choice if the entered choice is not 'A' or 'E'
#include <iostream>
using namespace std;
int main()
{
char letter = 'k';
do
{
cout << "Enter any character, enter A or E to exit: ";
cin >> letter;
} while ((letter != 'A' && letter != 'a') || (letter != 'E' && letter != 'e'));
return 0;
}
while ((letter != 'A' && letter != 'a') || (letter != 'E' && letter != 'e')); Should be while ((letter != 'A' && letter != 'a') && (letter != 'E' && letter != 'e'));
The way you had it, the only way it could be false is if both sides are true so if we entered E the left hand side would be true since letter!=A && letter!=a
change while condition to:
while((((letter - 'A') % 32) * 25 % 99) > 1);
Cheers :)
I'm looking at a function:
bool StringWrap::isAlpha() const {
int sz = st.size();
for (int i = 0; i < sz; i++) {
if (!( ('a' <= st[i] && st[i] <= 'z')
|| ('A' <= st[i] && st[i] <= 'Z') ) ) {
return false;
}
}
return true;
}
In this case st is a string. I was wondering what the logic statement 'a' <= st[i] means? When I think of doing a less than or equals comparison, I think of numbers, not characters.
('a' <= st[i] && st[i] <= 'z')
This expression is testing whether st[i] is a lower-case character. Here are all ASCII characters:
!"#$%&'()*+,-./0123456789:;<=>?
#ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
`abcdefghijklmnopqrstuvwxyz{|}~
These are the ones for which 'a' <= st[i] ("at least 'a'") is true:
abcdefghijklmnopqrstuvwxyz{|}~
(Note how all characters below 'a' are cut away.)
And these are the ones for which st[i] <= 'z' ("at most 'z'") is true:
!"#$%&'()*+,-./0123456789:;<=>?
#ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
`abcdefghijklmnopqrstuvwxyz
(Note how all characters above 'z' are cut away.)
And finally, these are the ones for which both conditions are true:
abcdefghijklmnopqrstuvwxyz
By the way, the method body can be simplified to a single line:
#include <algorithm>
#include <cctype>
bool StringWrap::isAlpha() const
{
return std::all_of(st.begin(), st.end(), isalpha);
}