Language signs and vector values - c++

#include "../../std_lib_facilities.h"
bool CheckForDislike(vector<string> disliked, string slowo) {
int i = 0;
while (i < disliked.size()) {
if (slowo == disliked[i]) {
return true;
}
i++;
}
return false;
}
int main() {
cout << "Enter a word: ";
vector<string> disliked(2);
disliked[0] = "Szpinak";
disliked[1] = "Brokuły";
string slowo = "a";
while (cin >> slowo) {
if (CheckForDislike(disliked, slowo)) {
cout << "Biip\n";
}
else {
cout << slowo << '\n';
}
}
}
Above code should print "Biip" when you enter a word that is in disliked vector.
It doesn't work for words with signs like "ą","ł" basically special signs from my native language. The function returns 1 or true for them, so the problem has to be somewhere in conditional, but i cant see why.

Related

Output "Valid" if input does not contain character 'S'. Otherwise, output "Invalid"

I am very new to C++ and am trying to complete an assignment involving loops. The goal of the activity is to print "valid" for passwords not containing the character "S" and "invalid" for those that do.
Here's the code:
#include <iostream>
using namespace std;
int main() {
bool validPassword;
string codeWord;
int passLength;
cin >> codeWord;
passLength = codeword.length();
for (int i = 0; i <= passLength; ++i) {
if (codeWord.at(i) != 'S') {
validPassword = true;
continue;
}
else {
validPassword = false;
break;
}
}
if (validPassword) {
cout << "Valid" << endl;
}
else {
cout << "Invalid" << endl;
}
return 0;
}
I'm kinda new to c++ as well, but this code I made worked for it. I used 'codeWord.find()' instead of cycling through all of the characters in the string. Here is the code:
#include <iostream>
using namespace std;
int main() {
bool validPassword;
string codeWord;
cin >> codeWord;
if(codeWord.find("8") == string::npos) {
validPassword = true;
}
else{
validPassword = false;
}
if (validPassword) {
cout << "Valid" << endl;
}
else {
cout << "Invalid" << endl;
}
return 0;
}

c++ problem with reverse and count string

I'm just learning c++. I have a problem with my program. I have to write a program which reverse string and count amount word in the string. My program doesn't return amount words and reverse only last word in string. I totally don't know how to correct it. :D
#include <iostream>
using namespace std;
void reverseString(string str)
{
for (int i=str.length()-1; i>=0; i--)
{
cout << str[i];
}
}
void countString(string strg)
{
int word = 1;
for(int j = 0; strg[j] != '\0'; j++)
{
if (strg[j] == ' ')
{
word++;
}
}
}
int main(void)
{
string inputString;
cout << "Give a string: ";
cin >> inputString;
cout << "Reverse string: ";
reverseString(inputString);
cout << "\nCounts words in a string: ";
countString(inputString);
return 0;
}
If you want to read multiple words then you must use getline as >> reads only a single word.
string inputString;
cout << "Give a string: ";
getline(cin, inputString);
To return something from a function you must 1) specify the return type and 2) use a return statement to return a value and 3) do something with that return value in the calling function
Step 1
int countString(string strg) // here we say countString returns an integer
{
...
}
Step 2
int countString(string strg)
{
...
return words; // here we say the value we want to return
}
Step 3
// here we output the value returned from the function
cout << "\nCounts words in a string: " << countString(inputString) << "\n";
Knowing how to write functions that return values is absolutely fundamental C++. You should practise this. See if you can do the same with your reverseString function, instead of printing a string make it return a string.
There are some mistake in your code.In countString() function you return nothing.So it does not print anything.If you take input as a string include a space character,please use getline(cin, inputString).Here the code for you:
#include <iostream>
using namespace std;
void reverseString(string str)
{
for (int i=str.length()-1; i>=0; i--)
{
cout << str[i];
}
}
int countString(string strg)
{
int word = 0;
for(int j = 0; strg[j] != '\0'; j++)
{
word++;
}
return word;
}
int main(void)
{
string inputString;
cout << "Give a string: ";
getline(cin, inputString);
cout << "Reverse string: ";
reverseString(inputString);
cout << "\nCounts words in a string: ";
cout<<countString(inputString)<<endl;
return 0;
}

Read new line character in fstream C++

How do I read the new line character? I am trying to do a character count, but the new line gets in the way. I tried doing if (text[i] == ' ' && text[i] == '\n') but that didn't work. Here is my repl.it session.
I am trying to read this from file.txt:
i like cats
dogs are also cool
so are orangutans
This is my code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream input;
input.open("file.txt");
int numOfWords = 0;
while (true)
{
string text;
getline(input, text);
for(int i = 0; i < text.length(); i++)
{
if (text[i] == ' ')
{
numOfWords++;
}
}
if (input.fail())
{
break;
}
}
cout << "Number of words: " << numOfWords+1 << endl;
input.close();
}
Your question is asking how to count characters, but your code is counting words instead. std::getline() swallows line breaks. You don't need to worry about them if you want to count words. In fact, you can use operator>> to greatly simplify your counting in that case, eg:
int main()
{
ifstream input("file.txt");
int numOfWords = 0;
string word;
while (input >> word)
++numOfWords;
cout << "Number of words: " << numOfWords << endl;
return 0;
}
If you really want to count characters instead of words, use std::ifstream::get() to read the file 1 character at a time, eg:
int main()
{
ifstream input("file.txt");
int numOfChars = 0;
int numOfWords = 0;
bool isInSpace = true;
char ch;
while (input.get(ch))
{
++numOfChars;
if (std::isspace(ch, input.getloc())) {
isInSpace = true;
}
else if (isInSpace) {
isInSpace = false;
++numOfWords;
}
}
cout << "Number of chars: " << numOfChars << endl;
cout << "Number of words: " << numOfWords << endl;
return 0;
}

Password String compare [C++]

Trying to do a password validation method in order to allow a user to use my program.
I was wondering how would someone would implement such a code that allows the program to compare two strings, going down all the letters and numbers to make sure everything matches, and then allows a user to access program controls after successfully entering in a password.
So, for example, if the password is "h3llo" and someone typed in "he" the program will output an error message saying that the password is incorrect.
Here is what I started with:
void checkPassword() {
cout << "Enter password: ";
string password = "H3110W0r1d";
int length = password.length();
int i;
string input;
cin >> input;
for (i = 0; i < length; i ++) {
if ((input[i].compare(length[i]))) == 1) {
if ((input[i].compare(length[i])) == 0) {
cout << "Error! Wrong password!";
} else {
cout << "Welcome!";
}
}
I've tried so many different ways, however, I can't seem to get it working. Any help on what I am doing wrong?
Simply just compare one by one.
void checkPassword() {
cout << "Enter password: ";
string password = "H3110W0r1d";
int length = password.length();
int i;
string input;
bool ok = true;
cin >> input;
if (input.length() != password.length()) {
ok = false;
} else {
for (i = 0; i < length; i ++) {
if (input[i] != password[i]) {
ok = false;
break;
}
}
}
if (ok) {
cout << "Welcome!";
} else {
cout << "Error! Wrong password!";
}
}
Or more simply
void checkPassword() {
cout << "Enter password: ";
string password = "H3110W0r1d";
string input;
cin >> input;
if (input == password) {
cout << "Welcome!";
} else {
cout << "Error! Wrong password!";
}
}

Checking if a string matches "END", and exit if it does

I have the following code below:
int main ()
{
char string[80];
bool Restart;
Restart = true;
while (Restart)
{
cout << "Enter a string\n" << endl;
cin.getline(string, 80);
cout << "\nYou entered: ";
printf(string, 80);
cout << endl;
int len=strlen(string);
bool flag = true;
for (int c=0; c!=len/2; c++)
{
if (flag)
{
if(string[c] != string[len-c-1])
{
flag = false;
}
}
else
{
break;
}
}
if (flag)
{
cout <<"\nThis is a Palindrome\n" << endl;
Restart = true;
continue;
}
else
{
cout << "\nThis is not a palindrome\n" << endl;
Restart = true;
continue;
}
cin.get();
}
}
I need to figure out a way to check if what the user entered is "END" in all caps, and if so, exit the program. I'm sure i can handle the exiting part, but I'm having trouble getting something that will check if the string entered is "END" in all caps.
Here is a solution that will terminate the while loop if "END" is entered. Create a helper method to do the parsing for END. Note this is very specific to the problem and not very reusable. It also is more difficult to maintain unlike Beta's solution, but you don't need to worry about maintenance until you start writing bigger programs.
bool Terminate(char* string, int len)
{
bool retval = false;
char* c = string;
if( 3 == len ) // "END\0"
{
if('E' == string[0])
{
if('N' == string [1])
{
if('D' == string[2])
{
retval = true;
}
}
}
}
return retval;
}
Then use it after int len=strlen(string); like so:
if(Terminate(string, len))
{
break; // Exit while loop
}
Make sure you declare Terminate before your main function
Is this enough of a hint?
#include <iostream>
int main()
{
char A[] = "END";
char B[10];
std::cin >> B;
std::cout << strcmp(A,B) << std::endl;
return(0);
}