1

I'm trying to remove all the spaces in the string. For example if the string has two spaces then it will reduce the two spaces to only one space.

The code which I'm trying is given below:

#include<stdio.h>

main()
{
    char s[] =  "Ahmed     is   not   here";
    int i,k,si = 0,n = 0,ln;
    ln = strlen(s);
    for(i=0;i<ln;i++)
    {
        n = s[i];
        if(n == 32)
        {
            si++;
            if(si>1)
            {
            s[i] = '-';
            si = 0; 
            }

        }

    }

    puts(s);
}

The problem is: I don't know how to remove that empty space so i'm replacing it with "-". Also, the code will delete the single spaces too. Please somebody correct my code and tell me where I was wrong.

Alex Johnson
  • 958
  • 8
  • 23
  • 2
    See https://stackoverflow.com/questions/1726302/removing-spaces-from-a-string-in-c – Norhther Aug 01 '18 at 15:37
  • 2
    You can copy it to a new string and jump all the ' ' caracters – Miguel Aug 01 '18 at 15:37
  • You can move all characters one or more indexes to the left and terminate the end of the String with `/0`. Another possibility would be that you copy all the `chars` and the single blanks to another array (you don't copy the other unneccessary blanks). – Alan Aug 01 '18 at 15:41
  • 2
    If the output required is `Ahmed is not here` with "only one blank" the duplicate does not solve it, but gives clues as to how to go about it, which in your case means remembering the "previous character". Then if the present character is a space, and the previous character is a space, you can ignore it. – Weather Vane Aug 01 '18 at 15:53

2 Answers2

1

There's another answer already on here that solves your problem. Here it is, co-opted into your application.

Code

#include <ctype.h>
#include <stdio.h>

void stripExtraSpaces(char* str) {
  int i, x;
  for (i=x=0; str[i]; ++i) {
    if (!isspace(str[i]) || (i > 0 && !isspace(str[i-1]))) {
      str[x++] = str[i];
    }
  }
  str[x] = '\0';
}

int main()
{
  char s[] =  "Ahmed     is   not   here";
  stripExtraSpaces(s);
  puts(s);
}

Explanation

The basic strategy is to iterate over each character in string, make sure it's not a space and that the character before it is not a space either, and then move to the next character. This skips duplicate spaces and only leaves single spaces in the string.

Note

As pointed out by WeatherVane below, this will also remove a single leading space. That may or may not be what you're looking for.

Alex Johnson
  • 958
  • 8
  • 23
  • Note that this removes a single leading space from a string . . . if that was required! A single leading space would be unnecessary but wasn't mentioned. – Weather Vane Aug 01 '18 at 16:19
  • @WeatherVane Good catch. I can't think of when I'd ever want to lead a string with a space, but maybe OP's requirements are different. – Alex Johnson Aug 01 '18 at 16:20
  • 1
    in case anyone wants it, same concept but using pointers and retaining a single leading space if presented with one or more [can be found here](https://ideone.com/g9Mdx6). – WhozCraig Aug 01 '18 at 16:47
0

This is a possible solution which I have found, in this code snippet below textis your sand blanks is your destination array which will be filled with the entire String but without the multiple blanks.

Code snippet:

#include <stdio.h>

int main()
{
   char text[1000], blank[1000];
   int c = 0, d = 0;

   printf("Enter your text"); //Ahmed     is   not   here
   gets(text);

   while (text[c] != '\0') {
      if (text[c] == ' ') {
         int temp = c + 1;
         if (text[temp] != '\0') {
            while (text[temp] == ' ' && text[temp] != '\0') {
               if (text[temp] == ' ') {
                  c++;
               }  
               temp++;
            }
         }
      }
      blank[d] = text[c];
      c++;
      d++;
   }

   blank[d] = '\0';

   printf("Text after removing blanks\n%s\n", blank);

   return 0;
}
Alan
  • 589
  • 5
  • 29
  • (1) **Never** use `gets`. It's so vile it has been removed from the standard library. The only place it remains is the nightmares of prior usage. (2) This is doable without a temporary buffer, and likely intended by whatever vague instructions the OP has received. The challenge is to make that reality. – WhozCraig Aug 01 '18 at 16:03
  • 1
    `text[temp] == ' ' && text[temp] != '\0'` is curious. Why not just `text[temp] == ' '`? I suspect a coding error here. – chux - Reinstate Monica Aug 01 '18 at 16:34