- 1

comparison in c

what possibly could be wrong here ? the comparison maybe ? #include <stdio.h> #include <string.h> #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { char *s; int i,m=0; int count; int rep=0; int arr[10]; scanf("%[^\n]",s); printf("%s",s); while(m<10){ count=0; for(i =0;s[i]!='\0';i++){ if(rep==s[i]) count++; } arr[m]=count; rep++; m++; } for(i=0;i<10;i++) printf("%d ",arr[i]); return 0; }

27th Oct 2022, 3:11 AM
Shimaa
Shimaa - avatar
17 Respuestas
+ 2
Apparently the task is to count frequencies of numeric characters. Dark the inner comparison needs to compare numeric characters ('0'-'9'). It is easy to convert the outer loop index m (0-9) into numeric characters. Simply add '0'+m. The outer loop has a known range, so a for loop is more appropriate than a while loop. for (int m = 0; m<=9; m++) { count = 0; for (i = 0; s[i]!='\0'; i++) { if('0'+m==s[i]) count++; } arr[m]=count; }
28th Oct 2022, 2:46 PM
Brian
Brian - avatar
+ 6
what is weird format specifier in scanf ? i havnt seen that before use scanf("%s",s); also you need to use fixed width array to get input or malloc-ed array .. not a char* .. becuse its a UB .. since pointer may point to any memory random address and accessing / writing on it can cause a seg fault .... so better do char s[1024]; or so ...
27th Oct 2022, 4:14 AM
Prashanth Kumar
Prashanth Kumar - avatar
+ 5
Dark, char *s is only a char pointer. Uninitialized, it holds an unpredictable memory address. Scanf would end up writing to that unplanned memory, causing unwanted results like memory violation, stack failure, program crash, ... etc. Dynamically allocate the array using alloc/malloc/calloc, or else preallocate it in an array declaration. Scanf presumes it can safely write to the memory location passed to it - and beyond.
27th Oct 2022, 4:43 AM
Brian
Brian - avatar
+ 5
Brian A read with "%[^s\n]" negated scanset will prevent a read in for character 's' and/or line break. The read will stop as any of the character listed in square bracket (past the ^) was found. The 's' inside the square bracket isn't necessary. http://www.cplusplus.com/reference/cstdio/scanf/
27th Oct 2022, 6:26 AM
Ipang
+ 4
Prashanth Kumar, most people use fgets, whereas Dark is using a little-known scanf specifier than lets you read an entire line, complete with spaces. EDIT: The rest of my answer is wrong. See Ipang's correction. Ignore this: [[The s is sandwiched between ^ and \n. The ^ represents the beginning of the line, so scanf reads everything between.]]
27th Oct 2022, 5:16 AM
Brian
Brian - avatar
+ 4
Brian Ooo.. Thanks
27th Oct 2022, 5:21 AM
Prashanth Kumar
Prashanth Kumar - avatar
+ 3
Ipang thank you for correcting me. I always thought it represented a beginning-of-line anchor as in regular expressions, but now I understand that would be without the bracket.
27th Oct 2022, 7:41 AM
Brian
Brian - avatar
+ 3
Dark, string characters are encoded in ASCII code - a standard of encoding that uses 1 byte per character and maps characters to values 0 - 127. The ASCII values of digits '0' - '9' are in the range 48 - 57. To the compiler, '0' and 48 are equal. But if you compare int values 0 - 9 with char values 48 - 57, the comparisons fail. To make the comparison work, you have to add the ASCII offset 48 to m to match the numeric characters. The expression '0'+m is just a more readable way to write 48+m, and it is easier than remembering the code for '0'.
29th Oct 2022, 6:12 AM
Brian
Brian - avatar
+ 2
Two major issues: 1) s needs to point to allocated memory or be declared as a char array. char s[10]; EDIT: I was wrong at first about the 2nd issue [[2) scanf is missing the string format specifier. Insert an s. scanf("%[^s\n]",s);]] Ignore this! Corrected: 2) The comparison should be if(s[m]==s[i])
27th Oct 2022, 4:08 AM
Brian
Brian - avatar
+ 1
Use the plus sign icon, Insert Code, to give a link to your code. It makes easier for people to try it, without having to copy and recreate it. https://code.sololearn.com/cQdDMp6ywLTH/?ref=app Can you explain in simple words, what your program is supposed to do? What kind of input do you expect?
27th Oct 2022, 4:02 AM
Tibor Santa
Tibor Santa - avatar
+ 1
It takes a. Line with letters and numbers And tell freq of each number , from 0 to 9 Tibor Santa
27th Oct 2022, 4:11 AM
Shimaa
Shimaa - avatar
+ 1
Dark it seems like it is best for you to declare an array as Prashanth Kumar and I suggested. Make sure it is large enough to hold all expected input.
27th Oct 2022, 5:22 AM
Brian
Brian - avatar
+ 1
Brian That input is readable and storaged , the problem is in comparison step , Why it's not functioning .?
27th Oct 2022, 6:24 AM
Shimaa
Shimaa - avatar
+ 1
i got u and it worked , thanks. could u please tell the logic about this " '0'+m==s[i] ? what does '0'+m actually does ? why it didn't work with m value directly ?
29th Oct 2022, 5:02 AM
Shimaa
Shimaa - avatar
0
Brian isnt char * s , a declaration for arry too ?
27th Oct 2022, 4:12 AM
Shimaa
Shimaa - avatar
0
@brain , how to make it ? the for loop >?
27th Oct 2022, 5:12 AM
Shimaa
Shimaa - avatar
0
here is what i meant but how can i make it smaller ? https://replit.com/@ShaimaaFikry/freq-of-each-num-in-a-sen#main.c @Brian
28th Oct 2022, 11:41 AM
Shimaa
Shimaa - avatar