School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,149 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,880 people online right now. Registration is fast and FREE... Join Now!



ecrypt type program

ecrypt type program Rate Topic: -----

#1 kokoro  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 29
  • Joined: 29-October 08


Dream Kudos: 0

Posted 29 November 2008 - 03:54 PM

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
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
 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 November 2008 - 04:29 PM

Was This Post Helpful? 0
  • +
  • -


#2 KYA  Icon User is offline

  • while(sad){!sad; awesome();}
  • Icon
  • View blog
  • Group: Mentors
  • Posts: 13,183
  • Joined: 14-September 07


Dream Kudos: 3000

Expert In: C, C++, Java

Posted 29 November 2008 - 05:17 PM

Where is speech declared? Also does this have to be done in C?

Here's an implementation I threw together, what a fun concept :)

#include <iostream>
#include <string>
using namespace std;

int main()
{

string word;
char* temp;
int rotation = 0;
cout << "Enter word: ";
getline(cin, word);
cout << "enter rotation #: " ;
cin >> rotation;
temp = new char[word.size()+1];
strcpy(temp, word.c_str());

for(int i = 0; i < word.size()+1; i++)
{
      temp[i] += rotation;
}

cout << "\nOriginal: ";
for(int i = 0; i < word.size() ; i++)
{
      cout << word.at(i) << " ";
}


cout << "\nEncrypted: ";
for(int i = 0; i < word.size()+1; i++)
{
      cout << temp[i] << " ";
}

cout << "\nDecrpyted: ";
for(int i = 0; i < word.size()+1; i++)
{
        temp[i] -= rotation;
      cout << temp[i] << " ";
}

delete[] temp;
system ("pause");
return 0;
}



Was This Post Helpful? 0
  • +
  • -

#3 kokoro  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 29
  • Joined: 29-October 08


Dream Kudos: 0

Posted 30 November 2008 - 11:28 AM

View PostKYA, on 29 Nov, 2008 - 05:17 PM, said:

Where is speech declared? Also does this have to be done in C?

Here's an implementation I threw together, what a fun concept :)

#include <iostream>
#include <string>
using namespace std;

int main()
{

string word;
char* temp;
int rotation = 0;
cout << "Enter word: ";
getline(cin, word);
cout << "enter rotation #: " ;
cin >> rotation;
temp = new char[word.size()+1];
strcpy(temp, word.c_str());

for(int i = 0; i < word.size()+1; i++)
{
      temp[i] += rotation;
}

cout << "\nOriginal: ";
for(int i = 0; i < word.size() ; i++)
{
      cout << word.at(i) << " ";
}


cout << "\nEncrypted: ";
for(int i = 0; i < word.size()+1; i++)
{
      cout << temp[i] << " ";
}

cout << "\nDecrpyted: ";
for(int i = 0; i < word.size()+1; i++)
{
        temp[i] -= rotation;
      cout << temp[i] << " ";
}

delete[] temp;
system ("pause");
return 0;
}



yea i'm learning c i'm guessing you did it in c++ cus i got no clue what cout does lol
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • while(sad){!sad; awesome();}
  • Icon
  • View blog
  • Group: Mentors
  • Posts: 13,183
  • Joined: 14-September 07


Dream Kudos: 3000

Expert In: C, C++, Java

Posted 30 November 2008 - 11:30 AM

cout is essentially the C++ version of printf, but its an object.
Was This Post Helpful? 0
  • +
  • -

#5 LaFayette  Icon User is offline

  • D.I.C Regular
  • Icon
  • Group: Contributors
  • Posts: 293
  • Joined: 24-November 08


Dream Kudos: 25

Posted 30 November 2008 - 12:37 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).
Was This Post Helpful? 0
  • +
  • -

#6 kokoro  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 29
  • Joined: 29-October 08


Dream Kudos: 0

Posted 30 November 2008 - 01:42 PM

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
.size() +1
do exactly?
and is this
fgets(rotation, 100, stdin);
right? is fgets the same thing even tho im gettin numbers only and not chars?
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • while(sad){!sad; awesome();}
  • Icon
  • View blog
  • Group: Mentors
  • Posts: 13,183
  • Joined: 14-September 07


Dream Kudos: 3000

Expert In: C, C++, Java

Posted 30 November 2008 - 02:02 PM

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?
Was This Post Helpful? 0
  • +
  • -

#8 kokoro  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 29
  • Joined: 29-October 08


Dream Kudos: 0

Posted 02 December 2008 - 03:41 PM

View PostKYA, on 30 Nov, 2008 - 02:02 PM, said:

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?
Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • while(sad){!sad; awesome();}
  • Icon
  • View blog
  • Group: Mentors
  • Posts: 13,183
  • Joined: 14-September 07


Dream Kudos: 3000

Expert In: C, C++, Java

Posted 02 December 2008 - 03:47 PM

To pick up single integers use scanf
Was This Post Helpful? 1
  • +
  • -

#10 KYA  Icon User is offline

  • while(sad){!sad; awesome();}
  • Icon
  • View blog
  • Group: Mentors
  • Posts: 13,183
  • Joined: 14-September 07


Dream Kudos: 3000

Expert In: C, C++, Java

Posted 02 December 2008 - 05:00 PM

I saw your message, if you need it in C# or java, i would recommend posting in the appropriate forum.
Was This Post Helpful? 0
  • +
  • -

#11 kokoro  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 29
  • Joined: 29-October 08


Dream Kudos: 0

Posted 02 December 2008 - 05:37 PM

View PostKYA, on 2 Dec, 2008 - 05:00 PM, said:

I saw your message, if you need it in C# or java, i would recommend posting in the appropriate forum.

lol no neither nor c++ using c only only language i'm learning atm
Was This Post Helpful? 0
  • +
  • -

#12 KYA  Icon User is offline

  • while(sad){!sad; awesome();}
  • Icon
  • View blog
  • Group: Mentors
  • Posts: 13,183
  • Joined: 14-September 07


Dream Kudos: 3000

Expert In: C, C++, Java

Posted 02 December 2008 - 06:28 PM

Oh ok, your message was confusing, one sec i'll see to porting it to pure C.

edited for a typo

use fgets(), jsut be careful to not overwrite the buffer

This post has been edited by KYA: 02 December 2008 - 06:38 PM

Was This Post Helpful? 0
  • +
  • -

#13 kokoro  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 29
  • Joined: 29-October 08


Dream Kudos: 0

Posted 03 December 2008 - 02:07 PM

	char speech[31];
	int rotation=0;
	char validSpeech;
	int validRotate;

printf("Please insert word and or phrase ");
fgets(speech, 30, stdin);

printf("Please insert rotation number between 1-5 ");
fgets(rotation, sizeof (rotation), stdin);
validRotate = validRotation; 

validRotation is a function in another source module that just checks to see if rotation is btw 1-5

I get an error right after I enter the word something runtime error telling me to abort so i guess my fget for my rotation is incorrect
Was This Post Helpful? 0
  • +
  • -

#14 KYA  Icon User is offline

  • while(sad){!sad; awesome();}
  • Icon
  • View blog
  • Group: Mentors
  • Posts: 13,183
  • Joined: 14-September 07


Dream Kudos: 3000

Expert In: C, C++, Java

Posted 03 December 2008 - 02:50 PM

fgets(speech, 31, stdin);



That should work:

Example:
#include <cstdio>
#include <cstdlib>
#include <string>
using namespace std;
void Encrypt(char arr[], int size, int rotation)
{
    for(int i = 0; i < size; i++)
    {
            arr[i] += rotation;
    } 
}

void Decrypt(char arr[] , int size, int rotation)
{
    for(int i = 0; i < size; i++)
    {
        arr[i] -= rotation;
    } 
}
int main()
{

//string word;
char* temp;
char arr[31];
int rotation = 0;
cout << "Enter word: ";
fgets(arr, 31, stdin); //C
printf("enter rotation #: ");
scanf("%d", &rotation);
temp = new char[31];
strcpy(temp, arr);

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: 03 December 2008 - 02:51 PM

Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month