String

Pages: 12
Guys I hope you are all doing well.
Im trying to build a program for some children to Learn how to write the word Banana.
The program should give output yes if the input is banana and no if the input is not banana.
Another thing is that it should ignore the repeated letters like bbbbaanaanaa should be considered as a yes.
And Bannaann should be considered as no because there's a missing "a" at the end.

I'm working on the code I'll submit later but if anyone has an idea please help.
THANKS IN ADVANCE
Last edited on
Well this seems like a worthwhile function to write.
1
2
3
4
5
6
7
8
9
string removeRepeatedLetters(string word) {
    // put some code here
}

int main ( ) {
    string test = "bbbbaanaanaa";
    string result = removeRepeatedLetters(test);
    cout << test << "=>" << result << endl;
}

No user interface, no Q&A from the users, no "yes or no" was it right.
Just focus on making a function do one specific thing, and do it well.

When it works, then you can start to add more stuff to main.
Thanks for the help..let me try to update it
I came up with this but I just don't know what to do next




void removeRepeatingChars(char* s){
if(!s[0]){
return;
}

int last=0;

for(int i = 1; s[i] ; i++){
if(s[i] != s[last]){
last++;
s[last] = s[i];
}
}
s[++last] = '\0';
}
Why don't you use salem c's example, I think it would save you quite the headache:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

std::string remove_repeated_letters(std::string word)
{
   /*Put things in here*/
}

int main ( ) 
{
    std::string input;
    
    std::cout << " Enter word to test : ";
    std::cin >> input;
    
    std::string result = remove_repeated_letters(input);
    std::cout << " Your word without repeated letters is : " << result
        << "\n Is your word \"banana\"?" << "\tThe answer is : ";
    
    if (result == "banana") std::cout << "Yes";
    else std::cout << "No";        
}

Output:
 Enter word to test : bbbbbbbbbbbaaaaaannnnnnnnaaaaaaaana
 Your word without repeated letters is : banana
 Is your word "banana"?	The answer is : Yes 
 Enter word to test : pinnnnneappplllle
 Your word without repeated letters is : pineaple
 Is your word "banana"?	The answer is : No


to help you out a bit here's how I would make the pseudocode:
>create a char array and initialise its content to the string you're passing to the function
>create an empty string to hold the word with no repeating letters
>for each char of the array, starting at the second char until the end of the array is reached.
>>if the previous char is different from current char, append previous char to new string
>>else do nothing
>return new string

EDIT: you could also mess with capital chars to put everything to lowercase but I don't think you're looking for that so far
Last edited on
Thanks so much
The instructions are very hard to me because English is not my born language if possible can u instruct me by using comments in the code. I would really appreciate that. Thanks
can u instruct me by using comments in the code.
An alternative is you write the comments in your language and we'll translate them to see if you understand the code. That way you're sharing the workload. BTW, your English isn't too bad at all.
The instructions are very hard to me because English is not my born language if possible can u instruct me by using comments in the code. I would really appreciate that. Thanks
What is your native language? and what do you need comments on, do you mean you want me to explain that:
>create a char array and initialise its content to the string you're passing to the function
>create an empty string to hold the word with no repeating letters
>for each char of the array, starting at the second char until the end of the array is reached.
>>if the previous char is different from current char, append previous char to new string
>>else do nothing
>return new string
?
Maybe:
> एक चार सरणी बनाएं और अपनी सामग्री को उस स्ट्रिंग पर इनिशियलाइज़ करें जो आप फ़ंक्शन में पास कर रहे हैं
> कोई दोहराए जाने वाले अक्षरों के साथ शब्द को पकड़ने के लिए एक खाली स्ट्रिंग बनाएं
> सरणी के प्रत्येक चार के लिए, सरणी के अंत तक दूसरे चार्ट पर शुरू करना।
>> यदि पिछला चार्ट वर्तमान चार्ज से अलग है, तो पिछले चार को नए स्ट्रिंग में जोड़ें
>> और कुछ नहीं करते
> नई स्ट्रिंग लौटाएं


Or,
> eagar cairr a chruthú agus a ábhar a thúsú leis an teaghrán a bhfuil tú ag gabháil dó don fheidhm
> cruthaigh teaghrán folamh chun an focal a choinneáil gan aon litreacha athrá a dhéanamh
> i gcás gach cairr den eagar, ag tosú ag an dara ruaig go dtí go sroichtear deireadh an eagar.
>> má tá an t-charr roimhe seo difriúil ó charr atá ann faoi láthair, cuir an t-eireaball le sreangán nua
ní dhéanann aon rud eile
> seol teaghrán nua ar ais
Well that's hindi and gaelic out of the way, I can do french too if needed, what will it be? C:
I'm from Tanzania and my native language is kiswahili which doesn't have some vocabularies needed to learn c++
Well I think it's okay and it's probably the same as other languages when it comes to learning programming languages. It was the same for me. You just need to get used to the English words and vocab to deal with C++, for instance here, you need to know what the following things are:
>a function
>to initialise, create
>an array
>a character
>a string
>to return
>to assign, add, append to
>if / else

But this vocabulary should be explained in detail with whatever you're learning with.

Are you taking classes or are you learning from a textbook?
I can do french too if needed
mon dieu, sacre bleu un francais.

Maybe you have a neighbour who can help out with this as a go between language:

> tengeneza safu ya chati na uanzishe yaliyomo katika tambo unayopita kwenye kazi
> tengeneza kamba tupu kushikilia neno bila barua zinazorudia
> kwa kila chati ya safu, kuanzia saa ya pili hadi mwisho wa safu hiyo imefikiwa.
>> ikiwa chati ya zamani ni tofauti na chati ya sasa, ongeza chati ya zamani hadi kamba mpya
>> kingine usifanye chochote
> rudisha kamba mpya
use more library:

#include <regex>


http://www.cplusplus.com/reference/regex
Last edited on
@H00G0 I'm not taking any classes I'm just learning through books and other resources on the internet. But with examples I understand more.
Like taking a program coded by someone and try to understand it.
That is how i Learn c++
I'm not taking any classes I'm just learning through books and other resources on the internet. But with examples I understand more.
Well for the following things, try to have a look at reference and to understand it:

functions : http://www.cplusplus.com/doc/tutorial/functions/
arrays :http://www.cplusplus.com/doc/tutorial/arrays/
statement and flow control : http://www.cplusplus.com/doc/tutorial/control/

Like taking a program coded by someone and try to understand it.
Many people, including myself, will tell you that the best way of learning is doing it yourself. Start with the simplest and work your way up. understanding others' code is great, but that doesn't mean you can write yours on your own.

If this task is too difficult for you, pick an easier one and come back to it later. I can provide the code for your function, but if you don't make it yourself you're not really learning anything :)
Last edited on
I would really appreciate the the function code. I know it's not the best way of learning but I'm trying my level best to understand c++ without additional help from a teacher.
Thanks in advance
You seriously mean you can't even start this and compare two strings?

1
2
3
4
string input;
cin >> input;
if(input == "banana")
...


This means nothing? C'mon, you can't be serious. If you are then just follow the sound and well-meaning advice you've already been given.


Pages: 12