Header Ads

34 - WAP to Replace Large Blank (Spaces) to Single Blank (Space) in C

This program to replace large blank (space's) with a single blank (space) in C

Program Code:

/* Program to replace large spaces with a single blank. */

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

void main()
{
 /*variable declaration*/
 char array[100];
 int idx = 0, jdx, num = 0;

 //fetching the requirements from user
 printf("\n Enter your Text  below: \n");

 while ((array[num] = getchar()) != '\n')
 {
  num++;
 }
 array[num] = '\0';

 while (idx < num)
 {
  if (array[idx] == ' ' && (array[idx + 1] == ' ' || array[idx - 1] ==' '))
  {
   for(jdx = idx; jdx < num; jdx++)
    array[jdx] = array[jdx+1];
   num--;
  }
  else
  {
   idx++;
  }
 }
        printf("Fixed String : \n");
        printf("%s\n", array);
 printf("\n");
}

Program Output: 

No comments