newbie help using c++ strings?

john

Active member
Joined
Sep 3, 2007
Messages
3,725
Reaction score
0
Points
36
Age
65
So I'm learning to use strings and I'm trying to make a small program where the user first input random characters. i.e:

abcdegfaereagd

the program will then check if all the characters of the alphabet are used and if not the program will then output error missing characters ***.





#include <string>
#include <iostream>
using namespace std;

int main() {


string one;
cin >> one;

// I'm not really sure what I should do next?
// Should I use for or while loop?

return EXIT_SUCCESS;
}
 
If you can use a char * your answer becomes pretty easy. Remember that all chars are really just numbers. You can compare each char to a number, see ascii table.
 
for(int i=65; i <= 122; i++){
if( one.find( (char)i ) == string::npos){
//add this character to a vector or list of missing characters
}
}

this tries to find each character from the alphabet in the string you have assigned to the variable 'one'. if find returns string::npos, the character is not anywhere in the string. in this case you will want to push it onto a vector you've created outside of the loop's scope.

then after the loop you will have a vector full of missing characters which you can iterate through and print accordingly.

you can change the range depending on if case is used
65 - 90 are ASCII codes for uppercase letters
91 - 122 are ASCII lowercase letters

good luck
 
Back
Top