What's wrong in this code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What's wrong in this code

Spy code encryption

6th Mar 2020, 6:28 AM
Ajith
Ajith - avatar
6 Answers
+ 1
#include <stdio.h> #include<string.h> int main() { char ajith[50]; scanf("%s",ajith); char delimis="%,+,*,=,1,2,3,4,5,6,7,8,9"; char *lone; lone=strtok(ajith,delimis ); int i; for(i=0;lone[i]!='\0';++i); for(i=i-1;i>=0;i--) printf("%c",lone[i]); return 0; }
6th Mar 2020, 6:28 AM
Ajith
Ajith - avatar
0
line char delimis="%,+,*,=,1,2,3,4,5,6,7,8,9"; is wrong: 1. delimis should be char pointer to the string of delimiters. In your code it is char. 2. delimiters in the string should be listed without comma. replace it with char *delimis="%+*=123456789"; strtok may return NULL if it doesn't find any delimiter and it will reach the end of the string. You should check lone on the NULL; if strtok has found delimiter in the string, it replaces it with a null character ('\0') and return poiner to it. lines for(i=0;lone[i]!='\0';++i); for(i=i-1;i>=0;i--) printf("%c",lone[i]); return 0; } have a bug: at this moment lone can be either NULL or point to null character. if it is NULL you will get access violation in the line for(i=0;lone[i]!='\0';++i); if it points to null character, line for(i=0;lone[i]!='\0';++i); has effect of i=0, as lone[0] is null character lines for(i=i-1;i>=0;i--) printf("%c",lone[i]); return 0; } can cause access violation if delimiter is the first character of the string
6th Mar 2020, 8:43 AM
andriy kan
andriy kan - avatar
0
Can u rewrite my code in my way and fix bugs??
6th Mar 2020, 12:25 PM
Ajith
Ajith - avatar
0
You didn't write your task explanation. What should code output?
6th Mar 2020, 12:27 PM
andriy kan
andriy kan - avatar
0
12D5÷/%L$#3RL66O5#W DLROW WORLD
10th Mar 2020, 5:35 PM
Ajith
Ajith - avatar
0
Symbols should be removed and string be displayed Then string should reversed
10th Mar 2020, 5:36 PM
Ajith
Ajith - avatar