View previous topic :: View next topic |
Author |
Message |
IMRobbie Newbie cheater
Reputation: 0
Joined: 26 Sep 2009 Posts: 24
|
Posted: Wed Nov 18, 2009 9:28 pm Post subject: [C++]Strings and characters |
|
|
How can you determined if a character entered is contained somewhere in a string?
I want the program to display, yes if the character is in the string.
|
|
Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
|
Back to top |
|
 |
IMRobbie Newbie cheater
Reputation: 0
Joined: 26 Sep 2009 Posts: 24
|
Posted: Mon Nov 23, 2009 3:14 pm Post subject: |
|
|
How would you check for multiple occurrences of the same letter in the string?
|
|
Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
|
Back to top |
|
 |
&Vage Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Jul 2008 Posts: 1053
|
Posted: Tue Nov 24, 2009 1:02 pm Post subject: |
|
|
Code: | int __fastcall countArray( DWORD lpvArray, UCHAR uString, size_t sizeOfArray)
{
int ret = 0;
for(int i = 0; i < sizeOfArray; i++)
{
if(!memcmp((const void*)(lpvArray+i), &uString, sizeof(uString)))
ret++;
}
return ret;
}
|
|
|
Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Tue Nov 24, 2009 1:11 pm Post subject: |
|
|
&Vage wrote: | Code: | int __fastcall countArray( DWORD lpvArray, UCHAR uString, size_t sizeOfArray)
{
int ret = 0;
for(int i = 0; i < sizeOfArray; i++)
{
if(!memcmp((const void*)(lpvArray+i), &uString, sizeof(uString)))
ret++;
}
return ret;
}
|
|
lmfao
I wonder what happens when sizeof( uString ) is something huge
actually, let's just consider the case when i = sizeOfArray - 1 and sizeof( UString ) is say.. 2 ?
|
|
Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Tue Nov 24, 2009 1:46 pm Post subject: |
|
|
&Vage wrote: | Code: | int __fastcall countArray( DWORD lpvArray, UCHAR uString, size_t sizeOfArray)
{
int ret = 0;
for(int i = 0; i < sizeOfArray; i++)
{
if(!memcmp((const void*)(lpvArray+i), &uString, sizeof(uString)))
ret++;
}
return ret;
}
|
|
what
|
|
Back to top |
|
 |
IMRobbie Newbie cheater
Reputation: 0
Joined: 26 Sep 2009 Posts: 24
|
Posted: Tue Nov 24, 2009 3:32 pm Post subject: |
|
|
I'm on the verge on making a hangman game, so I'm trying to figure out a way to do it.
This is what I got so far.
Code: |
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
string randWord();
int main()
{
string guessWord = randWord(); //sets the returning value of randWord function in guessWord
return 0;
}
string randWord()
{
string wordBankArray[3] = {"random", "words", "here"};
srand(static_cast<int> (time(0)));
int randWord = rand() % (2 + 1);
return wordBankArray[randWord];
} |
Basically, it just selects a string out of wordBankArray and sticks it in the string variable guessWord.
Does anyone have any suggestion to what to do next?
|
|
Back to top |
|
 |
Fendaril Cheater
Reputation: 0
Joined: 08 Nov 2009 Posts: 27
|
Posted: Tue Nov 24, 2009 4:16 pm Post subject: |
|
|
Think. In hangman if you enter in a letter it crosses out all occurences of that letter and reveals it. I believe in c++ strings can be indexed like arrays do something like
Code: |
String word=wordBankArray[0];//sets to random
//lets say we picked a
word[1]="";
//word is now equal to rndom.
|
This is a very vague example but add more things like checking if the word length is 0. Then the person got the word right.
|
|
Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Tue Nov 24, 2009 4:28 pm Post subject: |
|
|
Da0omph wrote: | Does anyone have any suggestion to what to do next? |
Ask for a letter, if the letter is in the word display the word with unmasked letters, else add to the "hangman counter". If the counter is above the limit, hang the bitch.
Also, this is C++ and not C. Try and think in classes.
|
|
Back to top |
|
 |
IMRobbie Newbie cheater
Reputation: 0
Joined: 26 Sep 2009 Posts: 24
|
Posted: Tue Nov 24, 2009 4:37 pm Post subject: |
|
|
Flyte wrote: | Da0omph wrote: | Does anyone have any suggestion to what to do next? |
Ask for a letter, if the letter is in the word display the word with unmasked letters, else add to the "hangman counter". If the counter is above the limit, hang the bitch.
Also, this is C++ and not C. Try and think in classes. |
You see, that's the trouble I'm having.
|
|
Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Tue Nov 24, 2009 4:56 pm Post subject: |
|
|
Da0omph wrote: | Flyte wrote: | Da0omph wrote: | Does anyone have any suggestion to what to do next? |
Ask for a letter, if the letter is in the word display the word with unmasked letters, else add to the "hangman counter". If the counter is above the limit, hang the bitch.
Also, this is C++ and not C. Try and think in classes. |
You see, that's the trouble I'm having. |
http://www.cplusplus.com/reference/string/string/replace/
|
|
Back to top |
|
 |
Fendaril Cheater
Reputation: 0
Joined: 08 Nov 2009 Posts: 27
|
Posted: Tue Nov 24, 2009 5:21 pm Post subject: |
|
|
Slugsnack wrote: | Da0omph wrote: | Flyte wrote: | Da0omph wrote: | Does anyone have any suggestion to what to do next? |
Ask for a letter, if the letter is in the word display the word with unmasked letters, else add to the "hangman counter". If the counter is above the limit, hang the bitch.
Also, this is C++ and not C. Try and think in classes. |
You see, that's the trouble I'm having. |
http://www.cplusplus.com/reference/string/string/replace/ |
Loop through the string. If the inputted letter equals to a letter in the string display it. It is not hard.
Like I said you can index Strings like arrays.
|
|
Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Tue Nov 24, 2009 5:44 pm Post subject: |
|
|
This is what I mean by "thinking in classes".
Code: | #include <iostream>
#include <string>
#include <set>
#include <algorithm>
#include <functional>
using namespace std;
class MaskedString {
const string m_string;
set<char> m_letters;
bool IsMasked(const char C) const {
return (m_letters.find(C) == m_letters.end());
}
public:
MaskedString(const string & Target)
: m_string(Target) {}
string Masked() const {
string ret(m_string);
replace_if(ret.begin(), ret.end(), bind1st(mem_fun(&MaskedString::IsMasked), this), '-');
return ret;
}
string Real() const {
return m_string;
}
void Unmask(const char C) {
m_letters.insert(C);
}
};
int main()
{
string random = "zorro";
MaskedString zorro(random);
zorro.Unmask('z');
zorro.Unmask('r');
cout << zorro.Masked() << endl;
cout << zorro.Real() << endl;
return 0;
} |
|
|
Back to top |
|
 |
Fendaril Cheater
Reputation: 0
Joined: 08 Nov 2009 Posts: 27
|
Posted: Tue Nov 24, 2009 5:51 pm Post subject: |
|
|
Flyte wrote: | This is what I mean by "thinking in classes".
Code: | #include <iostream>
#include <string>
#include <set>
#include <algorithm>
#include <functional>
using namespace std;
class MaskedString {
const string m_string;
set<char> m_letters;
bool IsMasked(const char C) const {
return (m_letters.find(C) == m_letters.end());
}
public:
MaskedString(const string & Target)
: m_string(Target) {}
string Masked() const {
string ret(m_string);
replace_if(ret.begin(), ret.end(), bind1st(mem_fun(&MaskedString::IsMasked), this), '-');
return ret;
}
string Real() const {
return m_string;
}
void Unmask(const char C) {
m_letters.insert(C);
}
};
int main()
{
string random = "zorro";
MaskedString zorro(random);
zorro.Unmask('z');
zorro.Unmask('r');
cout << zorro.Masked() << endl;
cout << zorro.Real() << endl;
return 0;
} |
|
For a project such as this you REALLY do not need a class.
|
|
Back to top |
|
 |
|