In: Computer Science
Program in C
Write a function that takes a string as an argument and removes the spaces from the string.
Find the code for the above question below, read the
comments provided in the code for better understanding. If found
helpful please do upvote this.
Please refer to the screenshot of the code to understand the
indentation of the code.
Copyable Code
#include<stdio.h>
int main(){
//variable to store the string
char s[100];
printf("\nEnter the string: ");
gets(s);
int count = 0;
// Traverse the given string. If current character
// is not space, then place it at index 'count++'
for (int i = 0; s[i]; i++)
if (s[i] != ' ')
s[count++] = s[i]; // here count is
s[count] = '\0';
printf("\nString after removing spaces : %s",s);
}
Screenshot
Output