+ 1

Symbols challenge: I need your help

I want to know what is the error in my code please. It was working but without printing a space. So I created another array to copy only the alphanumeric text . And printing what is inside array str2; But it went even worse than before.

18th Nov 2020, 6:41 PM
Amal Gil
Amal Gil - avatar
3 Answers
+ 1
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<ctype.h> int main(){ char str[50]; char str2[50]; // int i; printf("enter name:"); scanf("%[^\n]c",str); //you are forgot to add str and use 'c' not 's'.. // for(i=0;i<strlen(str);i++) // if(isalnum(str[i])) { strcpy(str2,str); //this is enough, but here happens copying of argument2 array str content to argument 1 array str2 so actuall you need strcpy(str2,str); not (str,str2) } printf("%s", str2); return 0; }
18th Nov 2020, 6:53 PM
Jayakrishna 🇮🇳
+ 1
amal 01 to read a line of characters, numbers and spaces you'd better use fgets. Then since you are storing numbers as char, you can't just print them. You need to subtract them 48 (that is the char value of the number 0) So since you need to do different things depinding on wether str[i] is a number or a letter, I suggest you to use isalpha and isdigit rather than isalnum And you don't need to create another array. You can print the letters/numbers straight away.
18th Nov 2020, 7:12 PM
Davide - avatar
Hey amal 01 you are not copying in str2 bkoz you are using it in the inverse way, it should be(string you wanna save in, string you wanna save from) so in your case strcpy(str2,str); also I think you can do the whole thing using a while loop, and it maybe gonna be easier
18th Nov 2020, 9:06 PM
Khaled ^^ خالد القريشي‎
Khaled ^^ خالد القريشي‎ - avatar