- 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; }
17 ответов
+ 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;
}
+ 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 ...
+ 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.
+ 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/
+ 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.]]
+ 4
Brian
Ooo..
Thanks
+ 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.
+ 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'.
+ 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])
+ 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?
+ 1
It takes a. Line with letters and numbers
And tell freq of each number , from 0 to 9
Tibor Santa
+ 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.
+ 1
Brian
That input is readable and storaged , the problem is in comparison step ,
Why it's not functioning .?
+ 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 ?
0
Brian isnt char * s , a declaration for arry too ?
0
@brain , how to make it ?
the for loop >?
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