Jump to content

How to 'move' a character?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
16 replies to this topic

#1
AxlD

AxlD

    Newbie

  • Members
  • Pip
  • 6 posts
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... :mad:

Can anyone help me?

Thank you and excuse my english...

#2
kmhosny

kmhosny

    Programmer

  • Members
  • PipPipPipPip
  • 133 posts
you can get the ascii of the character by casting it and then add the moving value you want
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

#3
AxlD

AxlD

    Newbie

  • Members
  • Pip
  • 6 posts
Of course! Thank you so much! You helped me greatly!

#4
kmhosny

kmhosny

    Programmer

  • Members
  • PipPipPipPip
  • 133 posts
you welcome
"Recursion is just a line of code"
-Karim Hosny-
My flickr

#5
AxlD

AxlD

    Newbie

  • Members
  • Pip
  • 6 posts
Well let's expand it a little, I wrote this program:

#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! :P Can anyone help?

Again thank you!

#6
kmhosny

kmhosny

    Programmer

  • Members
  • PipPipPipPip
  • 133 posts
#include<cstring>
you can read the string by
char c[50]={0};//array of characters of size 50 initialized with 0's
scanf("%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

#7
AxlD

AxlD

    Newbie

  • Members
  • Pip
  • 6 posts
OMG it works great!
Thank you very much! :thumbup:
Here's the 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?

#8
kmhosny

kmhosny

    Programmer

  • Members
  • PipPipPipPip
  • 133 posts
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("%[^\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 :)

Edited by kmhosny, 16 March 2010 - 11:37 AM.
adding note

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

#9
AxlD

AxlD

    Newbie

  • Members
  • Pip
  • 6 posts
Again you're great! I'm on my first steps to programming so I'll be back with more sophisticated questions. Till then! :cool:

#10
kmhosny

kmhosny

    Programmer

  • Members
  • PipPipPipPip
  • 133 posts
till we meet again :)
"Recursion is just a line of code"
-Karim Hosny-
My flickr

#11
lthreed

lthreed

    Newbie

  • Members
  • PipPip
  • 18 posts
You don't need to cast the char as an int, just do the addition on the char
('&%:9]!~}|z2Vxwv-,POqponl$Hjig%eB@@>}=<M:9wv6WsU2T|nm-,jcL(I&%$#"`CB]V?Tx<uVtT`Rpo3NlF.Jh++FdbCBA@?]!~|4XzyTT43Qsqq(Lnmkj"Fhg${z@> Ah Malbolge

#12
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
If you think you need ASCII for C or C++, you're wrong.