Encoding with random ASCII
I have to creat a program:
1.enter a line of text and convert to ASCII
2.encode the text by adding a random nunber to the ASCII char.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
unsigned int a[80],ran,n=0,t,len=0,asc[80],n1=0,n2=127;
char input[80],ch;
void main(){
scanf("%s",&input); //receive data input
len=strlen(input)
for(int i=0;i<len;i++){
asc[i]=input[i];
asc[i]+=ran;
}
(then print it by for loop)
}
how can i made a different random no. whenever i run it?
and space can't be converted in ASCII when printing while i input a space in text.
I didn't understood you...
If the problem is generating random numbers, you should look at this simple code:
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int num_ran(int max)
{
int num;
randomize();
num = rand() % max;
return num;
}
int main()
{
int rand_num;
rand_num = num_ran(30);
printf("The random number was: %d\n",rand_num);
return 0;
}
Re: I didn't understood you...
Quote:
Originally posted by Scripter
If the problem is generating random numbers, you should look at this simple code:
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int num_ran(int max)
{
int num;
randomize();
num = rand() % max;
return num;
}
int main()
{
int rand_num;
rand_num = num_ran(30);
printf("The random number was: %d\n",rand_num);
return 0;
}
problems:
includes conio.h, a nonstandard header, for no good reason.
uses randomize(), a nonstandard function.
uses the "rand() % n" method to generate random numbers in a certain range which can be quite inaccurate
Re: Re: I didn't understood you...
Quote:
Originally posted by moi
problems:
includes conio.h, a nonstandard header, for no good reason.
uses randomize(), a nonstandard function.
uses the "rand() % n" method to generate random numbers in a certain range which can be quite inaccurate
The code can't be run in VC++, it showed a error message:
error C2065: 'randomize' : undeclared identifier
why?
And can I use "time" to generate random numbers?
What's the code?
Re: Re: Re: I didn't understood you...
Quote:
Originally posted by stevenC
The code can't be run in VC++, it showed a error message:
error C2065: 'randomize' : undeclared identifier
why?
And can I use "time" to generate random numbers?
What's the code?
randomize is a NON standard function like moi said.
You can use time to create pseudo random numbers. Just call srand with time( NULL ) as the parameter and then call rand() and mod it with your limit.
Re: Re: Re: Re: I didn't understood you...
Quote:
Originally posted by MrWizard
randomize is a NON standard function like moi said.
You can use time to create pseudo random numbers. Just call srand with time( NULL ) as the parameter and then call rand() and mod it with your limit.
I still dont know how to embed it...
Is it correct as follows?There is still error in VC++.
Is there any C program run the following code without errors?
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
unsigned int a[80],ran,n=0,t,len=0,asc[80],n1=0,n2=127;
char input[80],output[80],ch;
int main(void){
/*->*/ srand( (unsigned)time( NULL ) );
printf("This is a text encoding program.\n");
printf("Please enter text wanted to be encoded.\n");
/*->*/ ran= srand() ; //a random no. is generated
printf("%d\n",ran);
gets(input); //receive data input
len=strlen(input); //calculate the length of text inputted
printf("There are %d characters inputted(include blank space).\n",len);
//
for(int i=0;i<len;i++){
asc[i]=input[i]; //convert char to int 1st
asc[i]+=ran; //encoding by addition with a random no.
while(asc[i]>n2){//the loop ensure the no. is inbetween ASCII range
if(asc[i]>=0){
asc[i]-=n2;
a[i]++;
}
}
}
printf("The text is encoded as follows:(include blank space) \n");
for(int j=0;j<len;j++) printf("%d ",asc[j]);
//printf("\n%d",ran);
printf("\n\n");
printf("Do you want to decode the statement above?(y/n): "); //ask user of decoding
scanf("%c",&ch); //receive command
if((ch=='y')||(ch=='Y')){ //if confirm, start decoding
for(int u=0;u<len;u++){ //return to original text for each character as follows:
if (a[u]!=0){
output[u]=a[u]*n2+asc[u]-ran; //(for char exceed ASCII before encode)
}
else
output[u]=asc[u]-ran; //(for char inbetween ASCII before encode)
}
printf("The original text is:\n");
for (int k=0;k<len;k++) printf("%c",output[k]); //print out the original text
printf("\n");
}
return 0;
}
error C2065: 'time' : undeclared identifier
error C2660: 'srand' : function does not take 0 parameters
Code tags added by kermi3