![]() |
Got stuck while making a program,help would be appreciated
I decided to make a program that would change the inserted text according to this pattern:Replacing every letter with the next one in the alphabet.For example:
"Cat"- "Dbu". I started and it went well up untill a point,my code looks like this so far: :
It seemed to work well,but when i tried to make the program to replace the letter "B" with "C",it replaced the allready replaced letter "B".I am a tottal begginer in C++ and with no experience in other programming languages so far. I found help some time ago here on another question,i sincerely hope that you guys will be helpfull again. |
Re: Got stuck while making a program,help would be appreciated
My guess is that you added another loop similar to the one you are using there, so firstly it proccesses all the and and changes them to b's, then goes back to the start of the string and checks for all the b's and changes them to c's. See where you are going wrong?
Personally i would try a slightly different approach which is much less code. Im sure you are aweare tha each character in the alphat bet is represented by a number depending on which encoding you are using for example ASCII 'a' is 97. ASCII 'b' is 98. using this logic we can rewrite you code to change all the letters to the next letter in the alphabet in 1 loop. I would change your for loop to something like this. :
Now firstly you will notice that the conition of the for loop has been change from x<100; to x<(zbor.length());, this means that the for loop will only iterate as many times as there are number of characters in the string.char((int(zbor[x])+1)). This may look a bit confusing if your new to C++ but its quite simple.firstly it changes the letter at zbor[x] to an integer, then adds one to this to get the next letter in the alphabet then the outside char() converts the number back into its character form before finally storing it back into zbor[x].Note this is only a quick solution and not a full solution. AS i have left a few things out for you to think about, 1) what happens ro any space's in the string? 2) Whats happens when you reach 'z'? 3) Other punctuation, will not remain the same. So you made need to add in a few more lines of code to check that it is a letter, and then when you reach z, is the next ccharacter in the alphabet A?. A small clue for you is that all of the characters in the alphabet lie between the number 65 (A) and 122 (z), assuming its ASCII. So perhaps you should check to see if the value of the letter is within there before changing it. Hope i didn't talk to much rubbish. Chris |
Re: Got stuck while making a program,help would be appreciated
To deal with some of the stuff Chris left you to think about, you might consider some of the functions available from the header <cctype>. isalpha() for instance will be easier to use than checking that the integer value of the character is between 65 and 122, since there are a couple characters in there which aren't letters. isupper() or islower() can quickly tell you the casing of the letter as well (though you can come up with a solution where the case doesn't matter; I leave that as a challenge to you :icon_wink:)
|
Re: Got stuck while making a program,help would be appreciated
Thanks guys i will get to work at once
|
Re: Got stuck while making a program,help would be appreciated
let us know how it turns out ;)
|
Re: Got stuck while making a program,help would be appreciated
use an if statement to verify that the input is between a and z. you might need two conditions (an || ) if you're also accepting A-Z. I don't remember exactly but its in the ASCII table. C++ might have a function that skips whitespace. If not you can easily make your own by reading the input into a String, then 'deleting' any whitespace. you could do this by making a char var = " "; then trying to match it against each part of the String.
|
Re: Got stuck while making a program,help would be appreciated
Quote:
code :
|
Re: Got stuck while making a program,help would be appreciated
The reason why your having trouble with space is not to do with the code that is changing the letter, but the method that you accept input.
cin only accepts 1 word upto a space character, so instead you should implement something like this :
EDIT: this is perhaps the better way ;) :
im really not thinking straight tonight getline() Chris |
Re: Got stuck while making a program,help would be appreciated
Quote:
Yes dude thank you,you are tottaly right,what was i thinking,i guess i am not thinking at all tonight,will fix that now,going to bed to be as fresh as i can be for tomorow. |
Re: Got stuck while making a program,help would be appreciated
The problem with 'z' is a logic check you need to make. It's a special case, since incrementing it the value doesn't loop around again. You'll probably want another if statement for this case.
|
| All times are GMT -5. The time now is 4:24 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC