Thread: ASCII addition

  1. #1
    Unregistered
    Guest

    Talking ASCII addition

    Could somebody please tell me how to use a loop to encrypt a string by adding 1 to the ASCII value of each character in the string? EX "apple" to "bqqmf"? Thank You very much

  2. #2
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    for(int x = 0; x < strlen(mystring); x++)
    {
    mystring[x]++;
    }
    My Website

    "Circular logic is good because it is."

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    Code:
    for(int x = 0; x < strlen(mystring); mystring[x++]++);
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    16

    Question what if...

    what if it's on 'z' does it go to 'a'?
    • 0927
    • a.k.a 0 9 two 7

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    nope, in that case you might want something like this:
    Code:
    char EncryptChar(char ch)
    {
        if (ch == 'Z')  return 'A';  // that is if you care about uppercase
        if (ch == 'z')  return 'a';
        return ch + 1;
    }
    
    void EncryptString(char *str)
    {
        while (*str != '\0')   *str = EncryptChar(*str++);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ASCII character with ASCII value 0 and 32
    By hitesh_best in forum C Programming
    Replies: 4
    Last Post: 07-24-2007, 09:45 AM
  2. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  3. Office access in C/C++ NOT VC++!! :)
    By skawky in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2005, 01:43 PM
  4. ascii values for keys
    By acid45 in forum C Programming
    Replies: 2
    Last Post: 05-12-2003, 07:13 AM
  5. Checking ascii values of char input
    By yank in forum C Programming
    Replies: 2
    Last Post: 04-29-2003, 07:49 AM