-
caesar cypher
I'm trying to create a program that first takes in a number that determines the amount of letters the second input (sentence) will go up. This is what I have so far:
Code:
#include <stdio.h>
#include <string.h>
int main(void){
int i=0,a=1;
char sent[100];
while(true){
scanf("%d",&a);
if(a == 0){
break;
}
gets(sent);
while(sent[i]){
if(sent[i] != " "){
sent[i] += a;
}
}
printf("%s",sent);
}
getchar();
return 0;
}
I get an error at the line: if(sent[i] != " "){
How come?
Also, even with that working, how come this program doesn't end up working ?
Thank you :).
-
Sent[i] is a char, not a string. You're trying to match a char with a string, and that won't work.
Perhaps you meant to use ' ', with only single quotation marks?
You need to declare true, and hopefully initialize it, also. When your shift of letters, takes one letter past Z (or z), you have no logic to handle bringing it around to A or a.
You'll need that.
-
better change your loop to be
Code:
while(scanf("%d",&a) == 1)
{
instead of
Code:
while(true){
scanf("%d",&a);
2. Do not use gets - read FAQ
3. you should initialize i and increment it for each string - so your i loop better to write as
-
Thanks for your replys. I've updated the program and now have partial success.
Code:
int main(void){
int i=0,a;
char sent[100];
while(scanf("%d",&a) != '0'){
fgets(sent,sizeof(sent),stdin);
for(i=0;sent[i];i++){
if(sent[i] == ' ')
sent[i] = ' ';
else if((sent[i] + a) > 'Z')
sent[i] = 'A' + ((sent[i] + a) - 'Z')-1;
else
sent[i] += a;
}
printf("%s\n",sent);
}
return 0;
}
I have a few problems.
When I put in a 0, the program should quit the loop and end, but in the output it just seems to skip a line and continue working.
When I put in a number, it automatically interprets it and then waits for the next input (which is the only thing that should be interpreted..
The encoding part works though. The program assumes only capital letters will be used.
-
scanf doesn't return the character scanned. It returns the number of successfully scanned codes. In your case, you want it to return 1, since you're scanning in one thing, anything else would mean that something went wrong.
You should instead be checking to see if a is zero.
Quzah.
-
Ok. So I added this:
Code:
while(scanf("%d",&a) && a != 0){
Like always, It still encodes the number itself which I don't get how to stop because it should ask for the next input.
For Example.. I put in 5 and hit enter and I get * on the next line.
Now this is where I usually put in the actual sentence to encode which it normally did, but now with the new condition added to the while() loop, when I put in the new sentence and hit enter the program exits.
But it actually also exits when it finds a 0, but doesn't work now.
Any idea?
Here's the full updated code:
Code:
int main(void){
int i,a;
char sent[100];
while(scanf("%d",&a) && a != 0){
fgets(sent,sizeof(sent),stdin);
for(i=0;sent[i];i++){
if(sent[i] == ' ')
sent[i] = ' ';
else if((sent[i] + a) > 'Z')
sent[i] = 'A' + ((sent[i] + a) - 'Z')-1;
else
sent[i] += a;
}
printf("%s\n",sent);
}
return 0;
}
-
Why don't you just use fgets to control your input, and look at the first character in the buffer to see if it's a zero?
Code:
while( fgets( sent, sizeof sent, stdin ) )
{
if ( sent[ 0 ] == '0' )
break;
...otherwise do stuff...
}
Quzah.
-
But for the problem the input must be: first line- the number to increment and the second line the sentence. So I don't see how the number could also be in the sent array.
-
You don't have to send it in the array. You could, but you don't have to. Just pull it out, then overwrite the array with the line.
Quzah.
-
I don't know if this is what you meant, but now the 0 terminates the program and the encoding the number has been fixed, but the actual interpreting of the sentence has totally screwed up. I'm assuming it has something to do with a being a character instead of an integer like it was before.. or something like that.. any idea? lol
Code:
int main(void){
int i,a;
char sent[100];
while(fgets(sent,sizeof(sent),stdin)){
a = sent[0];
if(a == '0'){
break;
}
else{
fgets(sent,sizeof(sent),stdin);
for(i=0;sent[i];i++){
if(sent[i] == ' ')
sent[i] = ' ';
else if((sent[i] + a) > 'Z')
sent[i] = 'A' + ((sent[i] + a) - 'Z')-1;
else
sent[i] += a;
}
printf("%s\n",sent);
}
}
return 0;
}
-
A few idea's for your program.
Code:
/* A simple Caesar shift */
#include <stdio.h>
int main(void){
char again, gar;
int i=0, a;
char sent[100];
do {
printf("\n What shall our offset be for the shift? Enter an integer: [1-25]");
scanf("%d",&a);
gar = getchar();
fgets(sent,sizeof(sent) - 2,stdin);
for(i=0;sent[i];i++) {
if(sent[i] == ' ')
continue;
else if((sent[i] + a) > 'Z')
sent[i] = 'A' + ((sent[i] + a) - 'Z')-1;
else
sent[i] += a;
}
printf("%s\n",sent);
printf("\n Encode again [y/n]? ");
scanf(" %c", &again);
gar = getchar();
}while(again == 'y' || again == 'Y');
printf("\n\n\t\t\t press enter when ready \n");
gar = getchar(); gar++;
return 0;
}
-
Is there any particular reason you've got a - 2 on your fgets line? It will always make sure there's room for a null character.
Quzah.
-
Aha, it converts 'a' into its ascii format. Lets see if I can fix this..
BTW, I don't understand what the gar = getchar() is for
-
Problem solved. I converted the character a to int and now it works great....
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void){
int i,a;
char sent[100];
while(fgets(sent,sizeof(sent),stdin)){
a = atoi(sent);
if(a == 0){
break;
}
else{
fgets(sent,sizeof(sent),stdin);
for(i=0;sent[i];i++){
if(sent[i] == ' ')
sent[i] = ' ';
else if((sent[i] + a) > 'Z')
sent[i] = 'A' + ((sent[i] + a) - 'Z')-1;
else
sent[i] += a;
}
printf("%s\n",sent);
}
}
return 0;
}
well except for one thing....
at the end of the encoded sentence it ads on the encoded number... for example when i put in
5
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
I get
YMJ VZNHP GWTBS KTC OZRUX TAJW YMJ QFED ITL*
the * is extra.. it's encoding the number... or maybe the enter... or maybe the null character? I'm not sure.
Other then that.. it works now.
BTW.. adak has the same problem in his version.
-
That's the newline character probably. Try something like isalpha, which is in ctype.h, so that you don't mung up punctuation.
Quzah.