Join 306,833 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,766 people online right now. Registration is fast and FREE... Join Now!
since learning in class isn't enough for me i've been taking questions from the internet and making a program for them and i found a cool one.. you allow the user to insert a word (30chars max) and then allow them to choose a rotate number (btw 1-5) and then the rotate number moves each char in the word to output an encrypted word
CODE
main (void) { char word[31];// store word room for null int rotation; //store users number char validword;// make sure its not number etc int validRotate;//make sure its btw 1-5
printf("Please insert word and or phrase "); fgets(speech, 100, stdin);
printf("Please insert rotation number between 1-5"); fgets(rotation, 100, stdin);
}
i'm making the validations atm but what i cant figure out is since you don't know how long the user word is then i cant do
CODE
word[1]=word[1]+3 //3 rotate number
to get say A to D... so i was wondering how one goes about doing that and also i think my second fgets is wrong but i only know how to do it that way. Its just suppose to output the encrypted word and the original thanks in advance!
This post has been edited by kokoro: 29 Nov, 2008 - 04:29 PM
What your doing is called a Caesar Cipher. Later on you could a write a program that encrypts a plaintext with the more sophisticated Vigenère Cipher method. You could also do decryption programs for both of them (the latter one will be a real challange).
let me see if i get what your doing.. your transferring over the word into a a char called temp using the strcpy and just by using the pre increment it'll boost each letter by whatever i value is placed to? what does the
CODE
.size() +1
do exactly? and is this
CODE
fgets(rotation, 100, stdin);
right? is fgets the same thing even tho im gettin numbers only and not chars?
size() + 1 is because a C++ string doesn't have the null terminator appended to the end like in C -> '\0'
So when I copy the elements over to the temp array I need to allocate an additional space in memory.
I make a temp one so that I can do other stuff with the original if I so wanted.
rotation will be a single integer, why do you need 100 psaces for it?
becuase in C i get the too few argument call error.. i dont no how to use fgets for just integer only know this way that i've done it for strings.. when you put the original into the temp array will that fill you in for the space you needed? say i cpy speech[101] into tempSpeech and the user only inputs 5 letters will it make tempSpeech into a 5 element array?
Encrypt(temp, 31, rotation); for(int i = 0; i <31; i++) { printf("%c", temp[i]); } Decrypt(temp, 31, rotation); cout << endl; for(int i = 0; i <31; i++) { printf("%c", temp[i]); } delete[] temp; system ("pause"); return 0; }
edit: The user has to fill it up though or you'll get un expected behavior. Which is why the string method from page 1 is better. Unfortunately you are restricted to C.
This post has been edited by KYA: 3 Dec, 2008 - 02:51 PM