• We have to develop a code that will delete one or more continuous or discontinuous blank space from the scanned string.
  • The resultant string will be stored in a different char array called – modified str.
  • We scan the input string until null character to identify the blank spaces present within the string.
  • In each iteration we check for blanks, if detected we check for further continuous blanks if any in the input text.
  • For every contiguous blank space encountered we increment the index ‘i’ of the source array as we need to place the next character encountered after the blank in the resultant string.



#include <stdio.h>

int main()
char *str1, modifiedstr[100];
int i, j, size = 100;

printf("Enter a string of your choice\n");
str1 = (char*)malloc(size);
getline(&str1, &size, stdin);

//Removing one or more blank spaces from string

for(i=0, j=0; str1[i] != '\0'; i++, j++)
if(str1[i] == ' ')
while(str1[i] == ' ' && str1[i+1] == ' ')

modifiedstr[j] = str1[i];

printf("String after removal of blank spaces is as follows: \n");

return 0;


Enter a string of your choice

Good   Morning !

String after removal of blank spaces is as follows:


Report Error/ Suggestion

Related Posts:


Interview Questions

CopyRight © 2020

CopyRight © 2020