top of page

Digits In a String In C

Updated: Feb 8, 2023



#include <stdio.h>


void main() {

char s[100]="12345123456789678900";

int c=0,c0=0,i=0,c1=0,c2=0,c3=0,c4=0,c5=0,c6=0,c7=0,c8=0,c9=0;

printf("\nString : %s\n",s);

for(i=0;s[i]!='\0';i++)

{

if((s[i]=='0')||(s[i]=='1')||(s[i]=='2')||(s[i]=='3')||(s[i]=='4')||(s[i]=='5')|| (s[i]=='6')||(s[i]=='7')||(s[i]=='8')||(s[i]=='9'))

{

//printf("%c ",s[i]);

if(s[i]=='0')

{

c0 = c0 + 1;

}

if(s[i]=='1')

{

c1 = c1 + 1;

}

if(s[i]=='2')

{

c2 = c2 + 1;

}

if(s[i]=='3')

{

c3 = c3 + 1;

}

if(s[i]=='4')

{

c4 = c4 + 1;

}

if(s[i]=='5')

{

c5 = c5 + 1;

}

if(s[i]=='6')

{

c6 = c6 + 1;

}

if(s[i]=='7')

{

c7 = c7 + 1;

}

if(s[i]=='8')

{

c8 = c8 + 1;

}

if(s[i]=='9')

{

c9 = c9 + 1;

}

c=c+1;

}

}

printf("\nTotal digits : %d",c);

printf("\nZero : %d",c0);

printf("\nOne : %d",c1);

printf("\nTwo : %d",c2);

printf("\nThree : %d",c3);

printf("\nFour : %d",c4);

printf("\nFive : %d",c5);

printf("\nSix : %d",c6);

printf("\nSeven : %d",c7);

printf("\nEight : %d",c8);

printf("\nNine : %d",c9);

}

Recent Posts

See All
Stack Using Queue

A stack can be implemented using two queues in C. The basic idea is to use one queue for enqueue operations and another for dequeue...

 
 
 
Queue Using Stack

A queue can be implemented using two stacks in C. The basic idea is to use one stack for enqueue operations and another for dequeue...

 
 
 
Queue Using Array

#include <stdio.h> #include<stdlib.h> #include<math.h> #define size 5 int queue[size]; int front = -1; int rear = -1; void enque(void);...

 
 
 

Komentar


bottom of page