+ 9

What is wrong in the code?

There is a problem with your keyboard: it randomly writes symbols when you are typing a text. You need to clean up the text by removing all symbols. Task: Take a text that includes some random symbols and translate it into a text that has none of them. The resulting text should only include letters and numbers. Input Format: A string with random symbols. Output Format: A string of the text with all the symbols removed. Sample Input: #l$e%ts go @an#d@@ g***et #l#unch$$ Sample Output: lets go and get lunch My code #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> int main() { char str[50]; int i=0,f=0; fgets(str,50,stdin); while(str[i]!='\0') { if(isalpha(str[i])||isspace(str[i])) { printf("%c",str[i]); } i++; } return 0; } One of the test case fails

14th Jan 2024, 9:17 AM
Sanjana
Sanjana - avatar
6 Respuestas
+ 8
You have forgotten the numbers.
14th Jan 2024, 9:39 AM
JaScript
JaScript - avatar
+ 6
and you might need to consider more than 50 characters.
14th Jan 2024, 9:44 AM
Lisa
Lisa - avatar
+ 5
Lisa any suggestions on how to set the memory according to user input?
14th Jan 2024, 3:41 PM
Sanjana
Sanjana - avatar
+ 4
Have you tried something like int n; scanf("%d", &n); So to first ask for how many characters will follow and then initialize the array accordingly? might also want to try getline() https://c-for-dummies.com/blog/?p=1112
14th Jan 2024, 3:57 PM
Lisa
Lisa - avatar
+ 2
Lisa thank you so much for all the answers and suggestions 😊
14th Jan 2024, 4:12 PM
Sanjana
Sanjana - avatar