C Program To Remove Leading Zeros
Last Updated :
29 Jul, 2022
Here, we will build a C Program to Remove leading zeros with the following 2 approaches:
- Using for loop
- Using strspn
To remove all leading zeros from a number we have to give the input number as a string.
Input:
a = "0001234"
Output:
1234
1. Using for loop
C
#include <stdio.h>
#include <string.h>
int main()
{
char a[1000] = "0001234" ;
int i, c = -1;
for (i = 0; i < strlen (a); i++) {
if (a[i] != '0' ) {
c = i;
break ;
}
}
for (i = c; i < strlen (a); i++) {
printf ( "%c" , a[i]);
}
return 0;
}
|
2. Using strspn
strspn: Returns the length of the first segment of str1 that exclusively contains characters from str2.
C
#include <stdio.h>
#include <string.h>
int main()
{
char * s = "0001234" ;
int n;
if ((n = strspn (s, "0" )) != 0 && s[n] != '\0' ) {
printf ( "%s" , &s[n]);
}
return 0;
}
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...