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 months ago.
Improve this question
Sorry, I know this has been asked before but I have read all the answers and nothing works! Please help.
When I check acutal and expected outputs, they match, but in check50 It gives an error message.
Here is the link for check50 results: https://submit.cs50.io/check50/6d939efb8a55e3fadec1c60952311f6198cd0eb0
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
char crypt(int k,char w)
{
if ('a'<= w && w <='z')
{
return (((w-'a')+ k)%26+'a');
}
else if ('A' <= w && w <= 'Z')
{
return (((w -'A') + k) % 26 + 'A');
}
else
{
return (w);
}
}
int main(int argc, string argv[])
{
//input check
if (argc != 2)
{
printf("Usage: ./caesar key\n");
return 1;
}
int lngg = strlen(argv[1]);
for (int i = 0; i < lngg; i++)
{
if (isdigit(argv[1][i]) == 0)
{
printf("Usage: ./caesar key\n");
return 1;
}
}
int key = atoi(argv[1])%26;
//input
string plain = get_string("plaintext: ");
//crpypt starts
printf("ciphertext: ");
int lng = strlen(plain)+1;
for (int a = 0; a < lng ;a++)
{
printf("%c", crypt(key, plain[a]));
}
printf("\n");
}
Hi, When I look expected and actual outputs, everything seems perfect but it errors
The problem is that you are also printing the terminating null character to standard output using printf. This character is not printable, so you cannot see it, but check50 does detect it and therefore reports that your program failed.
The loop
for(int a=0; a<lng ;a++)
{
printf("%c",crypt(key,plain[a]));
}
will run for strlen(plain)+1 iterations, because that is the value to which you set the variable lng.
You should instead set the value of lng to strlen(plain), in order to prevent the terminating null character from being printed.
#w is correct.
Also, code can avoid 2 passes down the string and do only 1.
// int lng = strlen(plain)+1;
// for (int a = 0; a < lng ;a++)
for (int a = 0; plain[a] != '\0'; a++)
Related
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.
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.
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.
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 5 years ago.
Improve this question
I want to multiply the numbers in a given string which has one or more spaces.
Example:
If i input 52 26 23
the output should be 31096.
I've written this code but its not working:
#include <stdio.h>
int main()
{
char input[30];
int i, num = 0, v = 1;
gets(input);
for (i = 0; input[i] != '\0'; i++)
{
if(input[i] == 32)
{
v = v * num;
if(input[i+1] != 32)
{
num = 0;
continue;
}
}
num = (num * 10) + (input[i] - 48);
}
printf("%d",v);
return 0;
}
Try this one
#include <stdio.h>
#include <string.h>
int main()
{
char str[30];
char *token;
long int mul = 1;
gets(str);
token = strtok(str, " ");
while (token != NULL)
{
mul = mul * atoi(token);
token = strtok(NULL, " ");
}
printf("%ld",mul);
return 0;
}
The problem lies in your nested if statement. Once it enters the first if statement, that means input[i]==32, therefore it can never enter the next if statement where input[i]!=32.
I also have some suggestions for improving readability. Instead of using numbers to represent the characters, use the character literals themselves!
Another thing, you only have space for 30 characters in your input buffer. If a user attempts to enter more than this, you will have a buffer overflow.
Lastly, if the user puts more than one space between numbers, the output will become 0. That may be something you may or may not want to handle.
Edit: Before, the call to gets was only grabbing characters up to the first whitespace character. I've fixed this issue with a format string in a call to scanf instead. Buffer overflow problem still applies. Also, it was not multiplying with the last parsed integer, so I added code for that after the loop. Another note, this will only work for non-negative integers, if that's something you weren't aware of initially. The code just assumes the input is nice.
Edit 2: support for input with any number of spaces before, between, or after inputs.
#include <stdio.h>
int main() {
char input[30];
int i, num = 0, v = 1;
scanf("%[^\n]s", input);
// skip leading spaces
for(i = 0; input[i] == ' '; i++);
// parse remaining input
while(input[i] != '\0') {
if(input[i] == ' ') {
v *= num;
num = 0;
// skip subsequent spaces
while(input[++i] == ' ');
continue;
}
num *= 10;
num += input[i] - '0';
i++;
}
// ignore trailing spaces
if(input[i - 1] != ' ') {
// get last parsed integer
v *= num;
}
printf("%i\n", v);
return 0;
}
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 7 years ago.
Improve this question
I am writing a program of string pallindrome, code is compiling successfully but on running it accepting the string but nothing after that, the output window stays on hold with cursor blinking, help me what is wrong with this code.
I am using dev-c++
gets(ch); // the program stops here
p=ch;
while(ch!='\0')
{ p++;
size++;
}
for(i=0;i<20;i++)
{
if(c[i]==c[j])
printf("string is pallindrome");
else printf("string is not pallindrome");
}
getch();
return 0;
}
Here is the problem:
while(ch!='\0')
ch is a char array, and you are comparing it with a single char.
Also, size is not initialised.
I would suggest something like this:
size=0;
while(ch[size]!='\0')
{ p++;
size++;
}
or, using the pointer method:
while(*p!=0)
{
p++;
size++;
}
Also, instead of printing inside the for loop (which would make it print several times), use a flag variable.
You only need one loop, for example while (i < i).
Look at this example that will do the job:
#include <stdio.h>
#include <string.h>
/* in c99 use <stdbool.h> instead*/
typedef int bool;
#define true 1;
#define false 0;
int main(void)
{
char ch[20];
puts("enter the string: ");
gets(ch);
size_t size = strlen(ch);
bool pallindrome = true;
int j = size-1;
int i = 0;
while (i < j)
{
if(ch[i] != ch[j]) {
pallindrome = false;
break;
}
++i;
--j;
}
if (pallindrome)
printf("\"%s\" is pallindrome\n", ch);
else
printf("\"%s\" is not pallindrome\n", ch);
getch();
return 0;
}