Reading string one character at a time.

This is a discussion on Reading string one character at a time. within the C++ Programming forums, part of the General Programming Boards category; Okay, so the problem is, I'm reading content from a file, into a string. I need a solution that will ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    22

    Reading string one character at a time.

    Okay, so the problem is, I'm reading content from a file, into a string. I need a solution that will read each character from the string, and pass it through a rot13 type of function.

    So basically, I just can't figure out how to read one character from said string, at a time.


    Any help is appreciated.

    Thanks.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So what have you done so fare?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    22
    Honestly, I know why you're asking that, and that is to make sure I'm not getting anyone to do all the work for me.

    Seeing as how I have no idea how to approach this problem, I don't have any code snippet involving it. Although I could post my whole program's source code, I choose not to, as it's a personal tool, and I don't see how it would relate to the current issue at hand.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That is indeed why I'm asking.

    So, my suggestion is that you look at what options there are for reading a char at a time.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    920
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
       std::string s;
       do
       {
          std::cout << "Please, if you will, enter a string of two characters or more." << std::endl;
          std::getline(std::cin, s);
       }while(s.size() < 2);
       std::cout << "Here's a hint\n"
                 << "\tfirst char: " << s[0]
                 << "\n\tsecond char: " << s[1] << std::endl;
    }
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    22
    Well, thanks for your replies anyways, but I ended up going about solving my problem a different, and maybe more complicated than necessary way, but it works. :P

    One more thing though, I have this rot13 function, which rotates a character 13 positions:
    Code:
    char rot13( char x ) {
        return ( x - 'A' + 13 ) % 26 + 'A';
    }
    Say I want to decrypt a character that is encrypted using this function; how do I go about modifying the above code snippet to accomplish this?

    I've already tried the obvious solution of using the opposite of the operators used, such as replacing the addition sign with a subtraction sign, etc. Although I didn't know what to replace the percent (%) sign with, I still didn't get the correct outcome.

    So if anyone could provide me with a solution, it would be greatly appreciated.

    Thanks in advance.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The whole point of rot13 is you don't change one single thing to decrypt.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    22
    Quote Originally Posted by tabstop View Post
    The whole point of rot13 is you don't change one single thing to decrypt.
    Well, at first I was going to say I already tried that, and still didn't get the desired outcome.

    Although it seems the problem was, that I replaced 'A' + 13 with 'A' + 18. After switching it back, it seems to be working fine.

    Thanks.

  9. #9
    Registered User
    Join Date
    Jun 2009
    Posts
    1
    The func rot13 just only encrypt 'A'~'Z',
    if your just want gain that, you can deal with it like this:
    Code:
    char decrypt(char x)
    {
    	return (x-13)<'A'?(x-13)+26:(x-13);
    }

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    22
    Just to be on the safe side, this shouldn't cause me any problems, correct?:

    Code:
    char rot13 ( char x ) {
    	if (( x == 'a' ) || ( x == 'b' ) || ( x == 'c' ) || ( x == 'd' ) || ( x == 'e' ) || ( x == 'f' ) || ( x == 'g' ) || ( x == 'h' ) || ( x == 'i' ) || ( x == 'j' ) ||
    	 ( x == 'k' ) || ( x == 'l' ) || ( x == 'm' ) || ( x == 'n' ) || ( x == 'o' ) || ( x == 'p' ) || ( x == 'q' ) || ( x == 'r' ) || ( x == 's' ) || ( x == 't' ) ||
    	  ( x == 'u' ) || ( x == 'v' ) || ( x == 'w' ) || ( x == 'x' ) || ( x == 'y' ) || ( x == 'z' )) {
    	  	return ( x - 'a' + 13 ) % 26 + 'a';
    	} else if (( x == 'A' ) || ( x == 'B' ) || ( x == 'C' ) || ( x == 'D' ) || ( x == 'E' ) || ( x == 'F' ) || ( x == 'G' ) || ( x == 'H' ) || ( x == 'I' ) ||
    	 ( x == 'J' ) || ( x == 'K' ) || ( x == 'L' ) || ( x == 'M' ) || ( x == 'N' ) || ( x == 'O' ) || ( x == 'P' ) || ( x == 'Q' ) || ( x == 'R' ) || ( x == 'S' ) ||
    	  ( x == 'T' ) || ( x == 'U' ) || ( x == 'V' ) || ( x == 'W' ) || ( x == 'X' ) || ( x == 'Y' ) || ( x == 'Z' )) {
    	  	return ( x - 'A' + 13 ) % 26 + 'A';
    	} else {
    		return ( x );
    	}
    }

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What happened to isalpha?

    And you can make the return statement a little better by adding 13 and then subtracting 26 if isalpha is false again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:19 AM
  3. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 11:32 AM
  4. Replies: 3
    Last Post: 11-03-2002, 02:14 AM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 10:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21