Approach:

  • 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.

 

Code:

#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] == ' ')
{
i++;
}
i++;
}

modifiedstr[j] = str1[i];
}

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

return 0;
}

Output:

Enter a string of your choice

Good   Morning !

String after removal of blank spaces is as follows:

GoodMorning!

Report Error/ Suggestion

Related Posts:

[yuzo_views]











CopyRight © 2019

CopyRight © 2019