C Program to Replace Two or More Consecutive Blanks By a Single Blank

Introduction

In this article, we will explore a C program that tackles the task to replace two or more consecutive blanks with a single blank.

In computer programming, efficiency and accuracy are crucial factors to consider.

Also Read: C Program To Read Two Files Simultaneously

This program will help improve the readability of text by eliminating unnecessary whitespace.

By following the steps outlined in this article, you will be able to enhance the performance of your text-processing applications and ensure a better user experience.

Also Read: Armstrong Number in C Programming

C Program to Replace Two or More Consecutive Blanks By a Single Blank

Also Read: C Program to Find the Inverse of 2×2 Matrix

Writing a C program to replace two or more consecutive blanks with a single blank is a relatively straightforward task.

By utilizing string manipulation techniques, we can achieve the desired outcome efficiently.

Also Read: C Program to Copy the Contents of One File into Another File

Here’s an example of such a program:

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

void replaceMultipleBlanks(char* str) {
    int i, j;
    int length = strlen(str);
    int count = 0;

    for (i = 0; i < length; i++) {
        if (str[i] == ' ' && str[i + 1] == ' ') {
            for (j = i + 1; j < length - count; j++) {
                str[j] = str[j + 1];
            }
            count++;
            length--;
            i--;
        }
    }
}

int main() {
    char text[] = "This   is   an   example    text.";
    
    printf("Original text: %s\n", text);
    replaceMultipleBlanks(text);
    printf("Modified text: %s\n", text);
    
    return 0;
}

In this program, we define a function called replaceMultipleBlanks that takes a string as an argument.

Also Read: Getchar and Putchar Function in C with Example

The function iterates through the string and checks for consecutive blanks. If two or more consecutive blanks are found, the function shifts the subsequent characters to the left to replace the extra blanks.

Finally, the modified string is printed.

Also Read: Best 5 Programs on Fibonacci Series in C

How Does the C Program Work?

The C program we discussed works by iterating through the input string and identifying consecutive blanks.

Once consecutive blanks are detected, the program removes the extra blanks by shifting the subsequent characters to the left.

Also Read: Program To Reverse a String in C using Pointer

Let’s break down the steps involved:

  1. The program starts by including the necessary header files, stdio.h and string.h, to handle input/output operations and string manipulation functions, respectively.
  2. The replaceMultipleBlanks function is defined, taking a string as its parameter.
  3. Inside the function, a loop is used to iterate through each character in the string.
  4. The loop checks if the current character is a blank and if the next character is also a blank. If this condition is true, it means that there are consecutive blanks.
  5. If consecutive blanks are found, an inner loop is used to shift the subsequent characters to the left, effectively replacing the extra blanks.
  6. The count variable keeps track of the number of blanks removed, and the length variable is decremented accordingly.
  7. Finally, the modified string is printed in the main function after calling the replaceMultipleBlanks function.

Also Read: Find the Runner Up Score | Hackerrank Solution

By following this program, you can easily replace two or more consecutive blanks with a single blank, enhancing the readability of your text.

FAQs

Q: Why is it important to replace consecutive blanks with a single blank?

It is essential to replace consecutive blanks with a single blank to improve the readability and aesthetics of the text. Multiple consecutive blanks can make the text appear cluttered and harder to understand. By reducing the number of blanks, the text becomes more visually appealing and easier to process.

Q: Can this program handle other whitespace characters besides blanks?

Yes, the program can handle other whitespace characters, such as tabs or newlines, by modifying the condition in the replaceMultipleBlanks function. Instead of checking for a blank (' '), you can modify the condition to check for other whitespace characters.

Q: Is this program case-sensitive?

No, the program we discussed is case-insensitive. It treats uppercase and lowercase characters as the same. If you want to make the program case-sensitive, you can modify it by considering the ASCII values of the characters.

Q: How can I integrate this program into my own text-processing application?

To integrate this program into your own text-processing application, you can encapsulate the logic into a function or a module. By calling this function or module when needed, you can ensure that consecutive blanks are replaced by a single blank, improving the quality of your text processing.

Q: Are there any limitations to this program?

The program we discussed performs the task of replacing consecutive blanks efficiently. However, it has a limitation when it comes to handling leading or trailing blanks. The current implementation does not remove leading or trailing blanks. If you want to handle those cases as well, you can extend the program accordingly.

Q: Can I use this program for languages other than English?

Yes, you can use this program for languages other than English. The concept of replacing consecutive blanks with a single blank applies universally, regardless of the language. By incorporating this program into your text-processing applications, you can enhance the readability of text in various languages.

Also Read: 25 Tricky Questions on Pointers in C: Explained and Answered

Conclusion

In this article, we explored a C program that replaces two or more consecutive blanks with a single blank.

By utilizing string manipulation techniques, this program helps improve the readability and aesthetics of text.

We discussed the implementation details, how the program works, and addressed common questions related to the topic.

By incorporating this program into your text-processing applications, you can ensure better user experiences and enhance the overall quality of your software.

Removing unnecessary blanks will make your text more visually appealing and easier to understand.