+ 5

What's the wrong in this code?

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> int main() { char phrase[100]; fgets(phrase,100,stdin); // convert everything to lower case for(int i = 0; phrase[i]; i++) { phrase[i] = tolower(phrase[i]); } char ciphertext[100]; char *alphabet = "abcdefghijklmnopqrstuvwxyz"; char *alphakey = "zyxwvutsrqponmlkjihgfedcba"; for(int i = 0; i < (int) strlen(phrase); i++) { // just add to ciphertext if space if (*(phrase + i) == ' ') { ciphertext[i] = *(phrase + i); } else { for(int x = 0; x < (int) strlen(alphabet); x++) { if (*(phrase + i) == *(alphabet + x)) { // get letter from the same index in alphakey ciphertext[i] = *(alphakey + x); } } } } printf("%s\n", ciphertext); return 0; } https://www.sololearn.com/coach/66?ref=app

10th Jun 2020, 4:22 AM
Coder Kay
Coder Kay - avatar
3 Respostas
+ 2
I am a c noob but it seems there are some garbage values of your ciphertext array that got printed to the screen. This hack passed all test cases: char ciphertext[100]; for(int i = 0; i <= 100; i++){ ciphertext[i] = 32; } [Late edit] I forgot to say: 32 == ' ' (space)
10th Jun 2020, 6:12 AM
Kevin ★
0
Sorry bro 9334583354 this is
13th Oct 2020, 1:18 AM
ScArt
ScArt - avatar
- 1
9334583394 bro this is my watsapp number please sms me
12th Oct 2020, 3:09 PM
ScArt
ScArt - avatar