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...
How to 'move' a character?
Started by AxlD, Mar 16 2010 01:07 AM
16 replies to this topic
#1
Posted 16 March 2010 - 01:07 AM
|
|
|
#2
Posted 16 March 2010 - 02:12 AM
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
-Karim Hosny-
My flickr
#3
Posted 16 March 2010 - 02:17 AM
Of course! Thank you so much! You helped me greatly!
#4
Posted 16 March 2010 - 02:18 AM
#5
Posted 16 March 2010 - 06:38 AM
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! :P Can anyone help?
Again thank you!
#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
Posted 16 March 2010 - 07:04 AM
#include<cstring>
you can read the string by
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
-Karim Hosny-
My flickr
#7
Posted 16 March 2010 - 11:04 AM
OMG it works great!
Thank you very much! :thumbup:
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?
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
Posted 16 March 2010 - 11:25 AM
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 :)
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
-Karim Hosny-
My flickr
#9
Posted 16 March 2010 - 11:38 AM
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
Posted 16 March 2010 - 11:42 AM
#11
Posted 16 March 2010 - 12:52 PM
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
Posted 19 March 2010 - 01:02 PM
If you think you need ASCII for C or C++, you're wrong.


Sign In
Create Account

Back to top









