How to use strspn In C Language
strspn: Returns the length of the first segment of str1 that exclusively contains characters from str2.
C
// C Program to Remove leading zeros // using strspn #include <stdio.h> #include <string.h> int main() { // input string char * s = "0001234" ; int n; // strspn->Returns the length of the first segment of // str1 that exclusively contains characters from str2. if ((n = strspn (s, "0" )) != 0 && s[n] != '\0' ) { // printing the string after eliminating the zeros printf ( "%s" , &s[n]); } return 0; } |
Output
1234
C Program To Remove Leading Zeros
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