Thread: Character to ASCII

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    17

    Character to ASCII

    Code:
    #include <stdio.h>
    
    
    void main()
    {
    	
    int i;
    int alpha[]={'a','b','c','d','e',
                         'f','g','h','i','j',
                         'k','l','m','n','o',
                         'p','q','r','s','t',
                         'u','v','w','x','y','z'};
    
    int *ptr=alpha;
            
    for (i=1;i<=26;i++)
        {
        printf("Element %d Contains Value:  %d\n\n",i, *ptr);
        ptr++;
        }
    }
    Basically i am trying to make a type of coding program, you would type in a word or sentence and the program would convert it into a jumble of letters by adding lets say 2 to the ASCII code and printing the result, then i have to create another program to convert it back, i am not looking for anyone to do this for me, otherwise i won't learn anything, just a few tips and some links to some good articles that would help me.

    thanks in advance

  2. #2
    Registered User
    Join Date
    Dec 2010
    Location
    Lucknow, India
    Posts
    72
    Lets take an Example with a Simple Character:

    Code:
    #include<stdio.h>
    void main()
    { char alpha='a';
     int b;
    b=alpha;
    b=b+2;
    alpha=b;
    printf("%c",alpha);
    }
    now use this Concept with a String. All The Best. :-)

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Be careful when you simply add 2 to the ASCII code because you could end up with non-printable or weird characters. You'd be better off restricting it to an alphabet and shifting around in it:
    Code:
    char alpha[] = "abcdefghijklmnopqrstuvwxyz";
    char *str = "this is the string to be encoded";
    char *c;
    int i;
    
    for(i = 0;str[i];++i)
      if((c = strchr(alpha, str[i])))
        str[i] = alpha[((c - alpha) + 2) % strlen(alpha)];
    
    puts(str);
    Basically, for each character in the string, you check to see if it's a character that exists in the alphabet. If it is, then you substitute that character with the character that's 2 positions down from it in the alphabet. The modulus operation is to account for characters that are near the end of the alphabet ('y' becomes 'a', 'z' becomes 'b'). Any character that doesn't exist in the alphabet would stay the same (spaces, punctuation, etc.) but you could easily add them to the alphabet or do some other kind of encoding on characters that don't exist in the alphabet.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by larrydeloafer View Post
    Code:
    #include <stdio.h>
    
    
    void main()
    {
    	
    int i;
    int alpha[]={'a','b','c','d','e',
                         'f','g','h','i','j',
                         'k','l','m','n','o',
                         'p','q','r','s','t',
                         'u','v','w','x','y','z'};
    
    int *ptr=alpha;
            
    for (i=1;i<=26;i++)
        {
        printf("Element %d Contains Value:  %d\n\n",i, *ptr);
        ptr++;
        }
    }
    Basically i am trying to make a type of coding program, you would type in a word or sentence and the program would convert it into a jumble of letters by adding lets say 2 to the ASCII code and printing the result, then i have to create another program to convert it back, i am not looking for anyone to do this for me, otherwise i won't learn anything, just a few tips and some links to some good articles that would help me.

    thanks in advance
    Google ROT-13 ... which is about the same thing you're trying to do.

    ROT13 - Wikipedia, the free encyclopedia

    Basically you just add or subtract from the characters the user types in...

    Code:
    char *Test = "This is a test string\0";
    
    for (int x = 0; x <strlen(Test); x++)
       Test[x] += 5;
    
    printf("%s",Test);
    Pretty easy actually.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by CommonTater View Post
    Basically you just add or subtract from the characters the user types in...

    Code:
    char *Test = "This is a test string\0";
    
    for (int x = 0; x <strlen(Test); x++)
       Test[x] += 5;
    
    printf("%s",Test);
    Pretty easy actually.
    Except that code doesn't work. You can't modify a string literal. Also, the letters at the end of the alphabet aren't going to look so hot. Did you not even pay attention to previous posts?
    If you understand what you're doing, you're not learning anything.

  6. #6
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    I can't believe no one's said, "use int main, not void main", yet, so I'll say it.

    Use int main, not void main.

    Cprogramming.com FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[])
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. syntax error when defining vectors
    By starkhorn in forum C++ Programming
    Replies: 5
    Last Post: 09-22-2004, 12:46 PM
  4. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 05:32 PM
  5. Replies: 1
    Last Post: 07-31-2002, 10:49 AM