What is wrong in this implementation of Caesar Cipher? [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to implement the Caesar cipher but I'm not getting the expected output. What's wrong with the code?
Key: 3
Input: Hello
Output I'm getting: KNUUX
Expected Output: KHOOR
Code:
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, string argv[])
{
if(argc!=2)
{
return 1;
}
int k, i;
k = atoi(argv[1]);
printf("Enter the String to Encrypt: ");
string s=GetString();
for(i=0; i<strlen(s); i++)
{
if('A'>=s[i]<='Z')
{
s[i]=((s[i] - 'A' + k)%26) +'A';
}
else if('a'>=s[i]<='z')
{
s[i]=((s[i] - 'a' + k)%26) +'a';
}
}
printf("The Encrypted Text is %s\n",s);
}

if('A'>=s[i]<='Z')
Is certainly not doing what you seem to be expecting.
You probably want:
if ( (s[i] >= 'A') && (s[i] <= 'Z') )

for(i=0; s[i]; ++i)
if(isalpha(s[i]))
s[i]=(toupper(s[i]) - 'A' + k)%26 +'A';

Related

Why I am not asked [i] times to input a string? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am currently doing CS - PSET2, and I wrote the code below.
Everything is working, but now that I am re-reading it, I don't understand why.
The doubt is with line 11: if(isdigit(argv [1][i])).
Let's say I write "35", first char is a number, so loop is true and it starts --> I have to write a string (Line 16).
Now program will check the second char, "5", it is a number, true --> I have to write a string again theoretically.
But why not? Why don't I get double results for everything?
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int main (int argc , string argv [])
{
if (argc == 2 && isdigit(argv [1]))
{
int n = strlen(argv [1]);
for (int i=0; i<n; i++)
{
if(isdigit(argv [1][i]))
{
string plaintext = get_string("Plaintext: ");
int key = atoi(argv[1]);
int l = strlen(plaintext);
for(int p=0; p <l; p++)
if isupper(plaintext[p])
{
printf("%c", (((plaintext[p] - 'A') + key) % 26) + 'A');
}
else if islower(plaintext[p])
{
printf("%c", (((plaintext[p] - 'a') + key) % 26) + 'a');
}
else
{
printf("%c", plaintext[p]) ;
}
return 0;
}
else
{
printf("Nope\n");
return 1;
}
}
}
else
{
printf("Nope\n");
return 1;
}
}
You have a return 0; statement inside the if that checks for a digit. This will cause it return before it can loop again. Just get rid of that return.

I keep getting this compiling error: ceasar.c:9:1: error: expected identifier or '(' { [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
Hey all I have been trying to do the Cs50 course with harvard and am doing the 2nd week Ceasar assignment. For some reason I can't seem to solve this error and I can't seem to locate what the problem is. I've tried changing the bracket style from { to [ and ( but that isn't working and as far as I can tell I've declared everything correctly. It may have something to do with the semi at the end of the int main() but when I remove it I get another error telling me it should be there. The error message is ceasar.c:9:1: error: expected identifier or '(' { referring to the { between the int num = and the k == argv. I've posted the code below. Any help would be appreciated, thanks!!
#include <stdio.h>
#include <cs50.h>
#include <string.h>
int main(int argc, string argv[]);
int num;
{
k == argv[2];
if(argc != 2)
{
printf("K not included in command");
return 1;
}
string s = get_string("Insert Lowercase Message:");
for (int i = 0, n = strlen(s); i < n; i++)
{
if (s[i] >= 'A' && s[i] <= 'Z')
{
num = s[i] - 'A';
}
else if (s[i] >= 'a' && s[i] <= 'z')
{
num = s[i] - 'a';
}
Output[i] = (num + k)%26;
}
printf("Secret message %s\n", output[i]);
}
The following lines are problematic:
int main(int argc, string argv[]);
int num;
{
k == argv[2];
if(argc != 2)
You need to remove the semicolon after main;
You need to mov int num to after the opening {;
You need a declaration for k;
You need to check the number of arguments before attempting to assign argv[2] to k;
If you are expecting 2 arguments (argc == 2), then the second argument is at argv[1], not argv[2];
You need to use = for assignment, not ==;
k is an int, whereas argv[1] points to a string representation of an integer value; you will need to use atoi or strtol to convert the contents of argv[1] to the equivalent integer value;
Putting that all together:
#include <stdlib.h> // for atoi
...
int main( int argc, char **argv )
{
int num;
int k;
if ( argc != 2 )
// error
k = atoi( argv[1] );
and proceed from there.
Some words of warning - the CS50 library grossly misrepresents how strings and string processing work in C. Do not expect anything you learn in this course with respect to strings to carry forward in other environments.

Having trouble with recursion [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I am trying to make a program which prints all the possible permutations of the string "A?B?AB?" via replacing question marks with A or B. I can't understand why my code works the way it does and need help improving it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* rec(char s[8]){
int i=0;
for(;i<strlen(s);i++){
if(s[i]=='?'){
s[i]='A';
rec(s);
s[i]='B';
rec(s);
s[i]='?';
}
}
for(int k=0;k<strlen(s);k++){
if(s[k]=='?')
i=-1;
}
if(i!=-1)
printf("%s\n",s);
return s;
}
int main(){
char s[8]="A?B?AB?";
rec(s);
}
This should do the trick:
#include <stdio.h>
#include <string.h>
void rec(char *str, int size, int depth){
for (int i = depth; i < size; i++){
if (str[i] == '?'){
str[i] = 'A';
rec(str, size, i + 1);
str[i] = 'B';
rec(str, size, i + 1);
str[i] = '?';
return;
}
}
printf("%s\n", str);
}
int main(){
char s[8] = "A?B?AB?";
rec(s, strlen(s), 0);
}
It's much like August's solution, but I did decide to do some looping until it found the next question mark. That should avoid having too big of a callstack, which could lead to stack overflow, with really big strings. (Note: I didn't test it, so there could still be some minor problems)
You only need to look at one character at a time. Try this:
#include <stdio.h>
void PrintPermutationsFrom(int i, char s[])
{
if (s[i] == '?') {
s[i] = 'A';
PrintPermutationsFrom(i + 1, s);
s[i] = 'B';
PrintPermutationsFrom(i + 1, s);
s[i] = '?';
} else if (s[i] != '\0') {
PrintPermutationsFrom(i + 1, s);
} else {
puts(s);
}
}
void PrintPermutations(char s[])
{
PrintPermutationsFrom(0, s);
}
int main(void)
{
char s[] = "A?B?AB?";
PrintPermutations(s);
return 0;
}
I can't understand why my code works the way it does and need help improving it.
The rec code does simply too much in that after having replaced a ? with both A and B and called itself, it continues iterating over s and generates further output. That is too much because after the first found ?, the recursive invocations already have handled all following ? and generated all arrangements. To correct this, just insert a break; after s[i]='?';.

What is wrong with my code for CS50 Caesar problem? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
This problem aims to apply Caesar Cipher. The goal is to encrypt a user defined message. The problem requires us to get the key using the command prompt. After getting the message and the key we must encrypt each character by moving by the key. e.g. if the key is 1 then A - B and b - c and z - a, if the key is 2 then a - c, D - F etc... I have made a very solid program and it seems very correct yet when I use the check50 to check my code it appears so many errors yet the outputs are the same... I just don't get what's wrong with my code..
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Usage: ./caesar key\n");
return 1;
}
for (int i = 0; argv[1][i] != '\0'; i++)
{
if (isalpha(argv[1][i]))
{
printf("Usage: ./caesar key\n");
return 1;
}
}
int key = atoi(argv[1]);
string ptext;
ptext = get_string("plaintext: ");
printf("ciphertext: ");
int n = strlen(ptext);
for (int i = 0; i <= n; i++)
{
if (isupper(ptext[i]))
{
printf("%c", (((ptext[i] + key) - 65) % 26) + 65);
}
else if (islower(ptext[i]))
{
printf("%c", (((ptext[i] + key) - 97) % 26) + 97);
}
else
{
printf("%c", ptext[i]);
}
}
printf("\n");
return 0;
}
link to check50
https://submit.cs50.io/check50/f7714d6b10c0b2e9fd1c1f01f4209195d8dc5163
You're outputting the null terminator character.
int n = strlen(ptext);
for (int i = 0; i <= n; i++)
You should only loop to i < n.

a program to reverse each word in a string( ive read the previous solutions but im looking for one using the functions i only here [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
i've wrote this c program but im always receiving the same inputed sentence as an output without any change! i've split every word in the string and then reversed their position but it didnt work well! any solutions please!
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main ()
{
char A[81][81];
int t=0,j=1,k=0,l;
puts("Input a sentence (max 80 character)");
gets(A[0]);
while (A[0][t]!='\0')
{
if(A[0][t]=='\32')
{
j++;
t++;
k=0;
}
A[j][k]=A[0][t];
k++;
t++;
}
for (l=j;l>0;l--)
{
printf("%s",A[l]);
}
getch();
}
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main(void){
char A[81][81] = {0};
int t=0,j=1,k=0,l;
puts("Input a sentence (max 80 character)");
scanf("%80[^\n]", A[0]);//'gets' has already been abolished, it should explore a different way.
while (A[0][t] != '\0'){
if(A[0][t] == ' '){
++j;
k=0;
while(A[0][t] == ' ')//Skip spaces
++t;
} else {
A[j][k++] = A[0][t++];
}
}
for (l=j;l>0;l--){
printf(" %s",A[l]);
}
puts("");
getch();
}

Resources