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...
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
Of course! Thank you so much! You helped me greatly!
you welcome
"Recursion is just a line of code"
-Karim Hosny-
My flickr
Well let's expand it a little, I wrote this program:
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!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; }Can anyone help?
Again thank you!
#include<cstring>
you can read the string by
}Code: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
OMG it works great!
Thank you very much!
Here's the code:
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?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; }
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 overflowsince 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
Again you're great! I'm on my first steps to programming so I'll be back with more sophisticated questions. Till then!![]()
till we meet again![]()
"Recursion is just a line of code"
-Karim Hosny-
My flickr
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks