📝⚡Beginner
Remove Leading Zeros
Strip leading zeros
Remove leading zeros from a numeric string.
Program Code
remove_leading_zeros.c
C
1#include <stdio.h>2#include <string.h>34void removeLeadingZeros(char str[]) {5 int i = 0;6 7 // Skip leading zeros8 while (str[i] == '0') {9 i++;10 }11 12 // If all zeros, keep one13 if (str[i] == '\0') {14 str[0] = '0';15 str[1] = '\0';16 return;17 }18 19 // Shift remaining characters20 int j = 0;21 while (str[i]) {22 str[j++] = str[i++];23 }24 str[j] = '\0';25}2627int main() {28 char str1[] = "00012345";29 char str2[] = "0000";30 char str3[] = "01020304";31 32 printf("Original: %s -> ", str1);33 removeLeadingZeros(str1);34 printf("Result: %s\n", str1);35 36 printf("Original: %s -> ", str2);37 removeLeadingZeros(str2);38 printf("Result: %s\n", str2);39 40 printf("Original: %s -> ", str3);41 removeLeadingZeros(str3);42 printf("Result: %s\n", str3);43 44 return 0;45}Output
Original: 00012345 -> Result: 12345
Original: 0000 -> Result: 0
Original: 01020304 -> Result: 1020304
Related Examples
Want to Learn More?
Explore our comprehensive tutorials for in-depth explanations of C programming concepts.
Browse Tutorials