View Single Post
Old Mar 30th, 2008, 1:36 PM   #2
Freaky Chris
Professional Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 448
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
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.

C++ Syntax (Toggle Plain Text)
  1. for(x=0;x<(zbor.length());x++)
  2. {
  3. zbor[x]=char((int(zbor[x])+1));
  4. }

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
__________________
Steven Skiena - Algorithms

,[->+>+<<]>>[-<<+>>]>++++++++[-<++++++++>]<+[-<->]>+<<[[-]+++++++++++++++.[-]>]>>[+++++++++.[-]],
brainf**k -- It's such a pretty language
Freaky Chris is offline   Reply With Quote