Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: How to 'move' a character?

  1. #1
    AxlD's Avatar
    AxlD is offline Newbie
    Join Date
    Mar 2010
    Location
    Grand Line
    Posts
    6
    Rep Power
    0

    Question How to 'move' a character?

    Hello and bravo for the great work you have done in this site!

    I have an exercise in C. The user has to type a character and the program will move the character X positions to right. For instance, if the user types 'A' and the X=4 then the character will become 'E'. But I have no idea how to move a character. I guess it has to do something with the ASCII number of the character but I don't know how to do it...

    Can anyone help me?

    Thank you and excuse my english...

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    kmhosny's Avatar
    kmhosny is offline Programmer
    Join Date
    Feb 2008
    Location
    Egypt
    Posts
    133
    Rep Power
    0

    Re: How to 'move' a character?

    you can get the ascii of the character by casting it and then add the moving value you want
    Code:
    int value=(int)'A';//get the ascii equivalent to A
    char moved=(char)(value+4);//get the char equivalent to ascii of A +4
    "Recursion is just a line of code"
    -Karim Hosny-
    My flickr

  4. #3
    AxlD's Avatar
    AxlD is offline Newbie
    Join Date
    Mar 2010
    Location
    Grand Line
    Posts
    6
    Rep Power
    0

    Talking Re: How to 'move' a character?

    Of course! Thank you so much! You helped me greatly!

  5. #4
    kmhosny's Avatar
    kmhosny is offline Programmer
    Join Date
    Feb 2008
    Location
    Egypt
    Posts
    133
    Rep Power
    0

    Re: How to 'move' a character?

    you welcome
    "Recursion is just a line of code"
    -Karim Hosny-
    My flickr

  6. #5
    AxlD's Avatar
    AxlD is offline Newbie
    Join Date
    Mar 2010
    Location
    Grand Line
    Posts
    6
    Rep Power
    0

    Re: How to 'move' a character?

    Well let's expand it a little, I wrote this program:

    Code:
    #include<stdio.h>
    
    int main()
    {
        char moved,c;
        int x, value;
    
        printf("Type a letter \n");
        scanf("%c", &c);
        printf("Type the number of positions for the letter to be moved \n");
        scanf("%d", &x);
    
        value= (int) c;
        moved=(char)(value+x);
    
        printf("the new letter is %c \n", moved);
    
        return 0;
    }
    What I want to do now is to read a string and convert each letter of the string x positions. Like a cryprography or something! Can anyone help?

    Again thank you!

  7. #6
    kmhosny's Avatar
    kmhosny is offline Programmer
    Join Date
    Feb 2008
    Location
    Egypt
    Posts
    133
    Rep Power
    0

    Re: How to 'move' a character?

    #include<cstring>
    you can read the string by
    Code:
    char c[50]={0};//array of characters of size 50 initialized with 0's
    scanf("&#37;s",c);
    for(int i=0; i<strlen(c);i++)
    {
       int value= (int) c[i];
       char moved=(char)(value+x);
    }
    }
    "Recursion is just a line of code"
    -Karim Hosny-
    My flickr

  8. #7
    AxlD's Avatar
    AxlD is offline Newbie
    Join Date
    Mar 2010
    Location
    Grand Line
    Posts
    6
    Rep Power
    0

    Talking Re: How to 'move' a character?

    OMG it works great!
    Thank you very much!
    Here's the code:

    Code:
    #include<stdio.h>
    #include<cstring>
    
    int main()
    {
        char moved;
        int x, value, i;
        char sentence[15]={0};
    
        printf("Type a sentence \n");
        scanf("%s", &sentence);
        printf("Type the number of position for each letter to be moved \n");
        scanf("%d", &x);
    
        for (i=0; i<strlen(sentence); i++)
        {
            value=(int) sentence[i];
            moved=(char) (value+x);
            printf("%c", moved);
        }
    
        return 0;
    }
    Just for the record I noticed that if I type 2 words (with a space between the words I mean) the results make no sense. Can I fix that?

  9. #8
    kmhosny's Avatar
    kmhosny is offline Programmer
    Join Date
    Feb 2008
    Location
    Egypt
    Posts
    133
    Rep Power
    0

    Re: How to 'move' a character?

    when you type more than a word separated with " " spaces it ignores what's next to that space as scanf uses space as delimiter -to be able to decide the end of the stream-
    you can type instead of scanf
    scanf("&#37;[^\n]",c);
    note: to make it clear the %[^\n] tells the scanf to ignore any character taken as input except the one in the [] which is \n so when it meets a \n it will stop taking the input.
    but in this case it's vulnerable to attack such as buffer overflow since you are making a simple cryptography program you should check this out
    Last edited by kmhosny; 03-16-2010 at 12:37 PM. Reason: adding note
    "Recursion is just a line of code"
    -Karim Hosny-
    My flickr

  10. #9
    AxlD's Avatar
    AxlD is offline Newbie
    Join Date
    Mar 2010
    Location
    Grand Line
    Posts
    6
    Rep Power
    0

    Talking Re: How to 'move' a character?

    Again you're great! I'm on my first steps to programming so I'll be back with more sophisticated questions. Till then!

  11. #10
    kmhosny's Avatar
    kmhosny is offline Programmer
    Join Date
    Feb 2008
    Location
    Egypt
    Posts
    133
    Rep Power
    0

    Re: How to 'move' a character?

    till we meet again
    "Recursion is just a line of code"
    -Karim Hosny-
    My flickr

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 7
    Last Post: 10-12-2011, 06:24 AM
  2. how to * move????
    By shah18 in forum C and C++
    Replies: 2
    Last Post: 10-11-2010, 04:34 PM
  3. Please help with my next move...
    By AsneJr in forum General Programming
    Replies: 4
    Last Post: 09-19-2010, 06:56 AM
  4. MOVE FILE
    By psm in forum Managed C++
    Replies: 0
    Last Post: 04-04-2010, 01:28 PM
  5. textarea move to next row
    By yonghan in forum HTML Programming
    Replies: 3
    Last Post: 04-03-2009, 02:46 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts