-2

I've been trying to get this code to remove spaces and replace them with '%' but I can't seem to get it to work. Could someone please tell me what I'm doing wrong?

Input: The fox jumped over the moon. Outcome: The fox jumped over the moon.

Desired outcome: Input: The fox jumped over the moon. Output: The%fox%jumped%over%the%moon.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SPACE ' '

int main()
{
   char string[100], *blank, *start;
   int length, c = 0, d = 0;

   printf("Enter a string\n");
   gets(string);

   length = strlen(string);

   blank = string;

   start = (char*)malloc(length+1);

   if ( start == NULL )
      exit(EXIT_FAILURE);

   while(*(blank+c))
   {
      if ( *(blank+c) == SPACE && *(blank+c+1) == SPACE )
      {}
      else
      {
         *(start+d) = *(blank+c);
     d++;
      }
      c++;
   }
   *(start+d) = '\0';

   printf("%s\n", start);

   free(start);     

 system("PAUSE");
 }
Florent
  • 391
  • 1
  • 12
aab
  • 1,643
  • 2
  • 13
  • 22

3 Answers3

1

What your code is actually doing is removing all 2 consecutives spaces. Replace if ( *(blank+c) == SPACE && *(blank+c+1) == SPACE ) by :

 if ( *(blank+c) == SPACE)
  {
  *(start+d) = '%';
  d++;
  }

If you want to also collapse multiple spaces, you need to add the previous code to your if/else statement

if ( *(blank+c) == SPACE && *(blank+c+1) == SPACE )
  {}
else if ( *(blank+c) == SPACE)
  {
  *(start+d) = '%';
  d++;
  }
  else
  {
     *(start+d) = *(blank+c);
 d++;
  }
  c++;
Florent
  • 391
  • 1
  • 12
  • also, this is not clear what you want to do with consecutives spaces. I edited your question to make them appear in input and outcome – Florent Nov 14 '14 at 15:09
  • thanks! Your edit is fine. Is there any way to output the number of spaces replaced as well? – aab Nov 14 '14 at 15:14
  • yes, create a global int variable and increment it in the `if ( *(blank+c) == SPACE)` section – Florent Nov 14 '14 at 15:18
  • I tried using if (blank<1){ return 1; count++; to count the number of spaces replaced but can't get it to work. could you please let me know what I'm doing wrong? – aab Nov 14 '14 at 17:31
  • blank is a pointer so I do not see the point in comparing it with 1. You should just declare `int count=0;` at the beginning and increment count with `count++;` inside `if ( *(blank+c) == SPACE) {...}` and at the end you can output with `printf("replaced %d spaces\n", count);` – Florent Nov 14 '14 at 17:36
  • It returns: replaced 0 spaces. – aab Nov 14 '14 at 17:46
  • your `count++` should be misplaced, help yourself ! – Florent Nov 14 '14 at 18:30
1

It seems hard to read your code.Please try to use some sensible variable names. Anyway below is the while loop which is working:-

   if ( start == NULL )
      exit(EXIT_FAILURE);

   while(*(blank+c))
   {
      if ( *(blank+c) != ' ' )
      {
          c++;
      }
      else
      {
          *(blank+c) = '%';
      }
   }
   *(blank+c+1) = '\0';

   printf("%s\n", blank);
ravi
  • 10,994
  • 1
  • 18
  • 36
0
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define SPACE ' '

int main(){
    char string[100], *start, pv;
    int c, d;

    printf("Enter a string\n");
    scanf("%99[^\n]", string);

    start = malloc(strlen(string)+1);
    if( start == NULL )
        exit(EXIT_FAILURE);

    pv = 0;//previous character
    for(d=c=0; string[c]; ++c){
        if(string[c] == SPACE){
            if(pv != SPACE)
                start[d++] = '%';
            pv = SPACE;
        } else {
            pv = start[d++] = string[c];
        }
    }
    start[d] = '\0';

    printf("%s\n", start);
    free(start);

    system("PAUSE");
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • Thanks it's working now. I tried using if (blank<1){ return 1; count++; to count the number of spaces replaced but can't get it to work. could you please let me know what I'm doing wrong? – aab Nov 14 '14 at 16:08
  • @aab Do you need a number of space that could be reduced? – BLUEPIXY Nov 14 '14 at 16:23
  • I mean the number of spaces replaced by the '%' symbol. AS is in: 'The fox jumped over the moon.' would output: The%fox%jumped%over%the%moon. spaces replaced: 5 – aab Nov 14 '14 at 16:31
  • @aab count the number when assign the '%'. – BLUEPIXY Nov 14 '14 at 16:34