Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Got stuck while making a program,help would be appreciated (http://www.programmingforums.org/showthread.php?t=15514)

Marijan Mar 30th, 2008 1:12 PM

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:
:

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. void Funkcija1(string zbor)
  6. { 
  7.     char x;
  8.     zbor[x];
  9.     for(x=0;x<100;x++)
  10.   {
  11.     if(zbor[x]=='a')
  12.     zbor[x]='b';
  13. }
  14.  
  15.     cout<<zbor<<endl;
  16.  
  17. }   
  18.  
  19.     int main()
  20.  
  21.     {
  22.         string Vs;
  23.         cin>>Vs;
  24.         Funkcija1(Vs);
  25.  
  26.         system("PAUSE");
  27.  
  28.         return EXIT_SUCCESS;
  29.  
  30. }


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.

Freaky Chris Mar 30th, 2008 1:36 PM

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.

:

  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

Jimbo Mar 30th, 2008 3:08 PM

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:)

Marijan Mar 30th, 2008 3:29 PM

Re: Got stuck while making a program,help would be appreciated
 
Thanks guys i will get to work at once

Freaky Chris Mar 30th, 2008 4:34 PM

Re: Got stuck while making a program,help would be appreciated
 
let us know how it turns out ;)

Fall Back Son Mar 30th, 2008 4:49 PM

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.

Marijan Mar 30th, 2008 5:54 PM

Re: Got stuck while making a program,help would be appreciated
 
Quote:

Originally Posted by Freaky Chris (Post 143229)
let us know how it turns out ;)

I managed to build this so far,the problem with typing other characters is solved,they remain the same as intended,but there is a problem with spaces and the letter z :S
code
:

  1. #include <iostream>
  2. #include <string>
  3. #include <ctype.h>
  4. using namespace std;
  5.  
  6. void Funkcija1(string zbor)
  7. {  char a,b,c;
  8.     char x;
  9.     zbor[x];
  10.     for(x=0;x<(zbor.length());x++)
  11.   {
  12.       if(isalpha(zbor[x]))
  13.       zbor[x]=((int(zbor[x])+1));
  14.  
  15. }
  16.  
  17.     cout<<zbor<<endl;
  18.  
  19. }   
  20.  
  21.     int main()
  22.  
  23.     {
  24.         string Vs;
  25.         cin>>Vs;
  26.         Funkcija1(Vs);
  27.  
  28.         system("PAUSE");
  29.  
  30.         return EXIT_SUCCESS;
  31.  
  32. }


Freaky Chris Mar 30th, 2008 6:08 PM

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

:

  1. char Vs[200];
  2. cin.getline(Vs, 200);


EDIT: this is perhaps the better way ;)

:

  1. string Vs;
  2. getline(cin, Vs);


im really not thinking straight tonight

getline()
Chris

Marijan Mar 30th, 2008 6:25 PM

Re: Got stuck while making a program,help would be appreciated
 
Quote:

Originally Posted by Freaky Chris (Post 143233)
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

:

  1. char Vs[200];
  2. cin.getline(Vs, 200);


EDIT: this is perhaps the better way ;)

:

  1. string Vs;
  2. getline(cin, Vs);


im really not thinking straight tonight

getline()
Chris


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.

Jimbo Mar 30th, 2008 8:46 PM

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