C program to trim leading and trailing white spaces from string

Category: C Program
Tags: #cprogram#string

Learn how to write a C program to trim leading and trailing white spaces from a string. This tutorial includes step-by-step instructions and code implementation to help you understand the process of trimming white spaces from a string using basic string manipulation techniques.

String manipulation is a fundamental part of programming in C. One common task is trimming leading and trailing white spaces from a string. This article will guide you through writing a C program to accomplish this task using basic string manipulation techniques without relying on the isspace function.

Steps to Solve the Problem

  1. Input the String: Read the input string from the user.
  2. Trim Leading White Spaces: Find the first non-white space character and shift the string accordingly.
  3. Trim Trailing White Spaces: Find the last non-white space character and truncate the string after it.
  4. Print the Result: Output the modified string.

Write a C program to trim leading and trailing white spaces from given string

#include <stdio.h>
#include <string.h>

int main() {
    char str[200];
    int start, end, i;

    // Input the string
    printf("Enter the string: ");
    fgets(str, sizeof(str), stdin);
    str[strcspn(str, "\n")] = '\0'; // Removing trailing newline character

    // Trim leading white spaces
    for (start = 0; str[start] != '\0'; start++) {
        if (str[start] != ' ' && str[start] != '\t' && str[start] != '\n' && str[start] != '\r') {
            break;
        }
    }

    // Trim trailing white spaces
    for (end = strlen(str) - 1; end >= 0; end--) {
        if (str[end] != ' ' && str[end] != '\t' && str[end] != '\n' && str[end] != '\r') {
            break;
        }
    }

    // Shift the trimmed string to the beginning of the original string array
    for (i = 0; start <= end; start++, i++) {
        str[i] = str[start];
    }
    str[i] = '\0'; // Terminate the string

    printf("Trimmed string: '%s'", str);

    return 0;
}

Output

Enter the string:          learn programming at ProCoding
Trimmed string: 'learn programming at ProCoding'

Explanation of the Code

  1. Input Handling: We read the input string using fgets and remove the trailing newline character using strcspn.
  2. Trim Leading White Spaces:
    • We iterate from the start of the string and skip over any white space characters (space ' ', tab '\t', newline '\n', and carriage return '\r').
    • start will point to the first non-white space character in the string.
  3. Trim Trailing White Spaces:
    • We iterate from the end of the string and skip over any white space characters (space ' ', tab '\t', newline '\n', and carriage return '\r').
    • end will point to the last non-white space character in the string.
  4. Shift and Truncate the String:
    • We shift the characters from the trimmed portion to the beginning of the original string array.
    • We terminate the string with a null character '\0'.
  5. Print the Result: We print the trimmed string.

Recommended Posts