1

I have a simple problem to solve:

Read a string, print the string without space and the number of spaces.

I could do this using 2 strings, one that will store the user string and other that will store the same string without spaces. But I would like to do this using only one string.

What I have so far:

while(str[i] != '\0'){
        if(str[i] == ' '){
            contEsp += 1;
        }else{
            strcpy(&str[i - contEsp], &str[i]);
        }
        i++;
    }

The problem:

It's not counting number of spaces.

If the user types double space or more, the program doesn't count and doesn't remove spaces.

Questions:

Whats the problem with my code?

Is it possible to do this using just one string?

undur_gongor
  • 15,657
  • 5
  • 63
  • 75
Kauê Gimenes
  • 1,278
  • 1
  • 13
  • 30

5 Answers5

3

Try this code:

int i = 0, contEsp =0;

while(str[i] != '\0')
{
    str[i-contEsp] = str[i];  

    if(str[i] == ' ')
      contEsp++;
    i++;       
}

str[i-contEsp] = '\0';

printf("String: %s, Spaces = %d\n",str, contEsp);
Subbu M
  • 194
  • 1
  • 5
  • In while loop, we should do character copying. But you were trying with strcpy & not avoiding spaces while copying (not changing the index value). Your code doesn't take care the String termination as well. – Subbu M Jun 08 '13 at 04:55
0

1) Last spaces don't be cut too. Add check and action after loop.

2) I recommend to divide notions of local space counter and global space counter.

unsigned int contEsp = 0;
unsigned int i = 0;
unsigned int localEsp = 0;

while(str[i] != '\0'){
    if(str[i] == ' '){
        contEsp += 1;
        localEsp += 1;
    }else if(contEsp) {
        strcpy(&str[i - localEsp], &str[i]);
        localEsp = 0;
        continue;
    }
    i++;
}

if ( localEsp > 0 )
    strcpy(&str[i - contEsp], &str[i]);
Boris
  • 597
  • 2
  • 10
0

Since the question is tagged with "performance": your approach is copying the whole remainder string whenever a space is encountered. Although it will probably not matter in reality, this is inefficient. Just process the characters one by one, e.g.:

unsigned remove_count_spaces(char *a) {
    char *b = a;
    unsigned ct = 0;

    do {
        if (*a == ' ') ct++;
        else *b++ = *a;
    } while(*a++);

    return ct;
}

... plus checking the possible wrap-around in ct.

undur_gongor
  • 15,657
  • 5
  • 63
  • 75
0
char* RemoveAndCountSpaces(char* s)
{
    int contEsp = 0;
    char* x = s;
    char* org = s;

    while(*s != '\0'){
        if(*s == ' '){
            contEsp++;
        } else {
            *x = *s;
            x++;
        }
        s++;
    }
    *x = *s;

    printf("%s\nSpaces Found: %d\n", org, contEsp);
    return org;
}
abelenky
  • 63,815
  • 23
  • 109
  • 159
0

Whats the problem with my code ?
1. Count is not initialized to 0
2. strcpy(&str[i - contEsp], &str[i]); is shifting the yet-to-be-processed string and then your i is not indexing the character you think.

Its possible to do this just using one string ?
Yes - below

int CountAndBlank(char *str) {
  size_t x = 0;   /* index through un-spaced string */
  size_t i;       /* Index through original string */
  for (i = 0; str[i]; i++) {
    if (!str[i] == ' ') {
      str[x++] = str[i];
    }
  }
  str[x] = '\0';
  return i - x;  /* Spaces removed is the difference */
}

...
int Count = CountAndBlank(buf);
printf("Number of spaces removed from '%s' is %d\n", buf, Count);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256