Hello everyone, I just wanted to give a bit of background for this piece of code before I ask my question. In this code the user has to enter a string of only lowercase letters, and if they don't the program will continually ask them to re-enter until they get it right. Next my piece of code would take the string entered by the user and analyze each individual character of the string with the alphabet. My question is, how do I put in an effective counter that counts every letter not used in the string? That is the last objective for me for this piece of code. So, for example, if the user enters "asdf" the program should say "22 letters not used." Another example would be if the user enters "assd" the program should clearly say "23 letters not used. Sadly this is the part I can't seem to get working, since no matter what I put in for a string it always outputs "26 letters not used. Any help would be greatly appreciated!
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string inp;
int l_inp, i, un, in;
un=0;
char alpha;
bool flag;
flag=false;
do
{
cout<<"Please Enter String of Lowercase Letters: ";
cin>>inp;
i=0;
while (inp[i]>=97 && inp[i]<=122)
i++;
if (inp[i]=='\0')flag=true;
l_inp=0;
if (inp[l_inp]=='\0')flag=false;
}
while (flag==false);
for (alpha='a';alpha<='z';alpha++)
{
for (in=0;inp[in]!='\0';in++)
{
inp[in];
}
if (inp[in]!=alpha)un++;
}
cout<<" "<<un<<" letters are not used in the string"<<endl;
return 0;
}
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
string inp;
int l_inp, i, un, in;
un=0;
char alpha;
bool flag;
flag=false;
do
{
cout<<"Please Enter String of Lowercase Letters: ";
cin>>inp;
i=0;
while (inp[i]>=97 && inp[i]<=122)
i++;
if (inp[i]=='\0')flag=true;
l_inp=0;
if (inp[l_inp]=='\0')flag=false;
} while (flag==false);
for (alpha='a'; alpha<='z'; alpha++)
{
for (in=0; inp[in]!='\0'; in++)
{
inp[in];
}
if (inp[in]!=alpha)un++;
}
cout<<" "<<un<<" letters are not used in the string"<<endl;
return 0;
}
I think the first step should be validating the input - like that.
1 2 3 4
bool ValidInput(string input)
{
// your code
}
To find out the number of unused characters you could create a string with all the lower-case characters. Then you create a variable num_unused_characters and set it to 0.
Then you loop through all the characters in the string with all the lower-case characters and check if the character is in the input string.
If not you increment the variable num_unused_characters.
You also could create a function this.
1 2 3 4
int NumUnusedCharacters(string input)
{
// your code
}
The main program could be as simple as:
1 2 3 4 5 6 7 8 9 10 11 12 13
int main()
{
string input;
do
{
cout << "\nPlease enter a string (only lower case): ";
cin >> input;
}while(! ValidInput(input);
cout << "The string contains " << NumUnusedCharacters(string input) << " characters";
}
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
string inp; //This is the home project of Eric Estrella
int l_inp, i, un, in;
un=0;
char alpha;
bool flag;
flag=false;
do
{
cout<<"Please Enter String of Lowercase Letters: ";
cin>>inp;
i=0;
while (inp[i]>=97 && inp[i]<=122)
i++;
if (inp[i]=='\0')flag=true;
l_inp=0;
if (inp[l_inp]=='\0')flag=false;
}
while (flag==false);
for (alpha='a';alpha<='z';alpha++)
{
for (in=0;inp[in]!='\0';in++)
{
inp[in];
}
std::size_t found = inp.find_first_not_of("abcdefghijklmnopqrstuvwxyz");
if (found!=std::string::npos)
{
un++;
}
}
cout<<" "<<un<<" letters are not used in the string"<<endl;
return 0;
}
So this is the code I have now made, but when I run it and I enter a string of lowercase letters it is always telling me "0 letters are not used in the string" now. Any idea what's wrong now?
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
string inp; //This is the home project of Eric Estrella
int l_inp, i, un, in;
un=0;
char alpha;
bool flag;
flag=false;
do
{
cout<<"Please Enter String of Lowercase Letters: ";
cin>>inp;
i=0;
while (inp[i]>=97 && inp[i]<=122)
i++;
if (inp[i]=='\0')flag=true;
l_inp=0;
if (inp[l_inp]=='\0')flag=false;
}
while (flag==false);
for (alpha='a';alpha<='z';alpha++)
{
for (in=0;inp[in]!='\0';in++)
{
inp[in];
}
std::size_t found = inp.find(alpha);
if (found!=std::string::npos)
{
un++;
}
}
cout<<" "<<un<<" letters are not used in the string"<<endl;
return 0;
}
Okay guys, I'm nearly there. I changed it up a bit more, and now whenever I input a string like say "adsf" the output is "4 letters are not used in the string" or if I input "addf" the output is "3 letters are not used in the string"
So as we can see it's not exactly counting the letters not used, but the letters being used. What would I have to change in order for it to actually work the way I intend?
> So as we can see it's not exactly counting the letters not used, but the letters being used.
> What would I have to change in order for it to actually work the way I intend?
1 2
// cout<<" "<<un<<" letters are not used in the string"<<endl ;
std::cout << 26 - un << " letters are not used in the string\n" ;
Avoid this kind of palooka stuff:
1 2
while( inp[i]>=97 && inp[i]<=122 ) // use std::islower
for (alpha='a';alpha<='z';alpha++) // there is no guarantee that characters 'a' to 'z' will have consecutive values
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
bool all_lower_case( std::string str ) // see: std::all_of
{
// for each char in str, if it is not a lower case character, return false
// http://www.stroustrup.com/C++11FAQ.html#forfor( char c : str ) if( !std::islower(c) ) returnfalse ;
returntrue ; // every character is a lower case character
}
std::string get_all_lower_case_str()
{
std::cout << "please enter a string containing only lower case letters: ";
std::string str ;
std::cin >> str ;
if( all_lower_case(str) ) return str ; // this is the string that we want
std::cout << "this string contains characters other than lower case letters.\n" ;
return get_all_lower_case_str() ; // try again
}
int cnt_unique_chars( std::string str )
{
// sort the string http://en.cppreference.com/w/cpp/algorithm/sort
std::sort( str.begin(), str.end() ) ;
// bring unique characters to the front http://en.cppreference.com/w/cpp/algorithm/uniqueconstauto end_of_unique_chars = std::unique( str.begin(), str.end() ) ;
return end_of_unique_chars - str.begin() ; // length of the sequence of unique characters
}
int main()
{
std::string str = get_all_lower_case_str() ;
constint unique_chars_used = cnt_unique_chars(str) ;
std::cout << 26 - unique_chars_used << " lower case characters are not used\n" ;
}
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
string inp; //This is the home project of Eric Estrella
int l_inp, i, un, in, ln;
un=0;
ln=26;
char alpha;
bool flag;
flag=false;
do
{
cout<<"Please Enter String of Lowercase Letters: ";
cin>>inp;
i=0;
while (inp[i]>=97 && inp[i]<=122)
i++;
if (inp[i]=='\0')flag=true;
l_inp=0;
if (inp[l_inp]=='\0')flag=false;
}
while (flag==false);
for (alpha='a';alpha<='z';alpha++)
{
for (in=0;inp[in]!='\0';in++)
{
inp[in];
}
std::size_t found = inp.find(alpha);
if (found!=std::string::npos)
{
un++;
ln--;
}
}
cout<<" "<<un<<" letters are used in the string"<<endl;
cout<<" "<<ln<<" letters are not used in the string"<<endl;
return 0;
}
Okay everyone, I deeply apologize for bothering you all with constant replies but I'm at the very end of my program and I would like to thank you all for the monumental help! With this piece of code it is working exactly the way I was intending it for EXCEPT whenever I input a string with a space such as "asd fg" the output is "3 letters are used in the string" and "23 letters are not used in the string" when in actuality, as we can clearly see, it should be "5 letters are used in the string" and "21 letters are not used in the string"
Any way on how to account for a string that has a space involved?
std::string remove_space( std::string str )
{
std::string result ;
for( char c : str ) if( !std::isspace(c) ) result += c ;
return result ;
}
std::string get_all_lower_case_str()
{
std::cout << "please enter a string containing only spaces and lower case letters: ";
std::string str ;
std::getline( std::cin, str ) ; // get a complete line, including spaces
str = remove_space(str) ; // throw the spaces away
if( all_lower_case(str) ) return str ; // this is the string that we want
std::cout << "this string contains characters other than lower case letters.\n" ;
return get_all_lower_case_str() ; // try again
}
#include <iostream>
#include <string>
#include <map>
usingnamespace std;
int main ()
{
string input;
// Get a string of only lower case letters from the user.
do
{
cout << "Please Enter String of Lowercase Letters: ";
getline(cin, input);
} while(input.find_first_not_of("abcdefghijklmnopqrstuvwxyz") != std::string::npos);
// Use a std::map to count the number of characters in the string.
std::map<char,int> characters;
// Insert letters into the map, use the integer part of the map to count how many of each letter was entered.
for(auto& itr : input)
characters[itr]++;
// The size of the map tells us how many individual characters were entered.
std::cout << "There were " << characters.size() << " distinct characters entered." << std::endl;
// Print the map, showing how many of each letter were entered.
for(auto& itr : characters)
std::cout << "There were " << itr.second << " character " << itr.first << " entered." << std::endl;
return 0;
}
Okay, I've done that and added a line below that that says-
inp = remove_space(inp);
But then I get an error that says-
remove_space was not declared in this scope
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
string inp; //This is the home project of Eric Estrella
int l_inp, i, un, in, ln;
un=0;
ln=26;
char alpha;
bool flag;
flag=false;
do
{
cout<<"Please Enter String of Lowercase Letters: ";
getline(cin, inp);
i=0;
while (inp[i]>=97 && inp[i]<=122)
i++;
if (inp[i]=='\0')flag=true;
l_inp=0;
if (inp[l_inp]=='\0')flag=false;
}
while (flag==false);
for (alpha='a';alpha<='z';alpha++)
{
for (in=0;inp[in]!='\0';in++)
{
inp[in];
}
std::size_t found = inp.find(alpha);
if (found!=std::string::npos)
{
un++;
ln--;
}
}
cout<<" "<<un<<" letters are used in the string"<<endl;
cout<<" "<<ln<<" letters are not used in the string"<<endl;
return 0;
}
To clarify, I have removed the remove_space function but I did make the change to line 16 that you mentioned cire. Now when I input a string with a space it simply asks me once again to input in another string, so now it's not accepting strings with spaces even if all the letters used are lowercase. What do I do now?