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

Introduction

In this post, I am going to write a c program to replace two or more consecutive blanks by a single blank in a given string. Basically, we are going to read a string. If that string has more than one blank space, our program will replace those blank spaces with a single space.

Also Read: Switch Case in C Program to Calculate Area of Circle and Triangle

Before writing this program, let us see the expected output for this program.

Expected Output

c program to replace two or more consecutive blanks by a single blank output

As you can see in the output, we have read a string HPlus Academy for C Programming. But, there are more than one blank space between HPlus and Academy, for and C, C and Programming. In the final output, we have replaced these blank spaces with a single space.

Logic of this program

  1. Read a string through keyboard.
  2. Scan each and every character using while loop.
  3. In the loop, count total number of spaces. For this, we can use one counter variable i.e. countspace. When we count the space, then using continue statement we will go to the beginning of the loop.
  4. If the value of this countspace variable is greater than 1 i.e. one blank space, then we will print the next character after one single space.

Also Read: C Program to Print Multiples of 5 using do-while loop

Now, let us go to understand the actual program.

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

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char str[100];
    int i=0,countspace=0;;
    printf("Enter any String\n");
    gets(str);
    while(str[i]!='\0')
    {
        if(str[i]==' ')
        {
            countspace++;
            i++;
            continue;
        }
        if(countspace>1)
        {
            printf(" %c",str[i]);
            countspace=0;
        }
        else
        {
            printf("%c",str[i]);
        }
        i++;
    }
    return 0;
}

Explanation

Here, I am going to explain the important parts of the above program.

while(str[i]!='\0')

This is the while loop. This loop will be executed until we get \0.

if(str[i]==' ')
{
    countspace++;
    i++;
    continue;
}

We are reading each and every character from the given string. So blank space is also a character. We will get a minimum of one blank space. But what if we get more than one blank space. In this code, we are just checking if there is more than one blank space and we will come to know this using this countspace variable because we are going to increment this every time we get the blank space.

Also Read: C Program to Remove Zeros from a number

if(countspace>1)
{
    printf(" %c",str[i]);
    countspace=0;
}

In the above code, we are checking whether the value of the variable countspace is greater than one or not. If yes then we will print a single space followed by the next character. That is why I have given one single space before %c.

Also Read: The while loop in C Programming

After this, we will make the value of the variable countspace zero because we will check the next consecutive blank space if there are any.

else
{
    printf("%c",str[i]);
}

This is else part of the above if statement. This part will be executed when we have not got more than one consecutive blank space. Here, I have not given any space before %c.

So this is a c program to replace two or more consecutive blanks by a single blank space. I hope you have understood this program. If you have any difficulty understanding this program, then you can contact me.

Thank you.

Some Important C Programs

  1. Program in C to Find Longest Line in a File
  2. Palindrome in C using Pointers
  3. Insert and Delete element in Array in C using switch case
  4. C Program to Add Alternate Elements of a 2D Array
  5. Arrays in C for Complete Beginners
  6. C Program to Find Area of a Circle using Preprocessor
  7. C Program to Remove White Spaces and Comments from a File
  8. C Program to Print Numbers Except Multiples of n
  9. Reverse a Number using getchar and putchar function in c
  10. The while loop in C Programming

Leave a Comment