I am trying to write a C program that takes a string input from a user and then looks into the input to count the frequency of all the integers in the string. So suppose if the user gives the input:
a11472o5t6
the output would be :
0 2 1 0 1 1 1 1 0 0
my approach involves comparing every character of the string to all 10 digits one by one and if any the character is equal to the digit, it would increment the isdigit number by 1. the code I wrote for the same is as follows:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int is1 = 0; //initialise integers for checking
int is2 = 0;
int is3 = 0;
int is4 = 0;
int is5 = 0;
int is6 = 0;
int is7 = 0;
int is8 = 0;
int is9 = 0;
int is0 = 0;
int main()
{
char s[100]; //initialise array for string
scanf("%s", s); //scan string input
//now all characters of the string are stored in the array s
for (int i = 0; i < strlen(s); i ) //loop to iterate over all the elements in the array
{
if (strcmp(&s[i], "0") == 0)
{
is0 = is0 1;
}
if (strcmp(&s[i], "1") == 0)
{
is1 = is1 1;
}
if (strcmp(&s[i], "2") == 0)
{
is2 = is2 1;
}
if (strcmp(&s[i], "3") == 0)
{
is3 = is3 1;
}
if (strcmp(&s[i], "4") == 0)
{
is4 = is4 1;
//printf("%d", is4);
}
if (strcmp(&s[i], "5") == 0)
{
is5 = is5 1;
}
if (strcmp(&s[i], "6") == 0)
{
is6 = is6 1;
}
if (strcmp(&s[i], "7") == 0)
{
is7 = is7 1;
}
if (strcmp(&s[i], "8") == 0)
{
is8 = is8 1;
}
if (strcmp(&s[i], "9") == 0)
{
is9 = is9 1;
}
}
printf("%d ", is0);
printf("%d ", is1);
printf("%d ", is2);
printf("%d ", is3);
printf("%d ", is4);
printf("%d ", is5);
printf("%d ", is6);
printf("%d ", is7);
printf("%d ", is8);
printf("%d ", is9);
}
I expected the code to iterate over and over for the entire length of the string and and update values of the isdigit series every time a number was successfully found. However whenever I run the code only the last digit seems to find its place in the output .
for example if I type 54
as an input the expected output is
0 0 0 0 1 1 0 0 0 0
however the output my code seems to be giving is
0 0 0 0 1 0 0 0 0 0
likewise the number 45
also has the same expected output
0 0 0 0 1 1 0 0 0 0
but the output I am receiving is
0 0 0 0 0 1 0 0 0 0
which looks like the code overwrites any operation that occurred in the previous iteration, but I can't seem to understand why and how to fix it.
On my part I checked if the characters were being called properly one by one and that all characters were being compared, where I found no problem. I also looked up other answers on stack overflow and elsewhere, but was I am a beginner and most answers were written in reference to languages that I can't understand, so I was unable to relate to the solution they were being told. the closest I got was someone who was using a single variable repetitively thus overwriting it in each iteration. however I have declared sufficient variables( one for each digit from 0-9), so that shouldn't be the problem in my code either.
while I know this question could be solved easily using arrays, I would like to know what I was doing wrong here to avoid any such mistakes in the future.
CodePudding user response:
When you do if (strcmp(&s[i],"1")==0)
you are comparing strings, not individual characters, which is why only the last character is counted. It's the only one matching.
Example:
If s == "a11472o5t6"
and you use strcmp(&s[1], "1")
, you would be comparing the string "11472o5t6"
with the string "1"
, which clearly will not be equal.
You want if(s[i] == '1')
etc. to do the comparisons of individual characters instead.
And you are correct about using arrays instead. It'd certainly be easier.
Example:
#include <ctype.h>
#include <stdio.h>
int main() {
const char *str = "a11472o5t6";
int ints[10] = {0};
for (const char *chptr = str; *chptr != '\0'; chptr) {
if(isdigit((unsigned char) *chptr)) ints[*chptr - '0'];
}
for (int i = 0; i < 10; i) printf("%d %d\n", i, ints[i]);
}
Output:
0 0
1 2
2 1
3 0
4 1
5 1
6 1
7 1
8 0
9 0
CodePudding user response:
Try this:
#include <stdio.h>
#include <string.h>
int main(){
char s[100];
scanf("%s",s);
char arr1[10] = {'0','1','2','3','4','5','6','7','8','9'};
int arr2[10] = {0};
for(int i = 0; i < strlen(s);i ){
for(int j = 0; j < 10; j ){
if(s[i] == arr1[j])
arr2[j] ;
}
}
for(int i = 0; i < 10; i )
printf("%d ", arr2[i]);
}