-2

I need to read the text and find if there is more than one space between the words.

If there is change it to one.

For example if I have a text:

My         name     is      Lukas

Program should change it to:

My name is Lukas

Any ideas?

Mat
  • 202,337
  • 40
  • 393
  • 406
Lukas M.
  • 163
  • 1
  • 2
  • 13

5 Answers5

1
while (*str) {
  if (*str != ' ' || str[1] != ' ') *newstr++ = *str;
  str++;
}

*newstr = 0;
Déjà vu
  • 28,223
  • 6
  • 72
  • 100
  • See [a previous answer](http://stackoverflow.com/questions/14702877/recursive-form-of-changing-multiple-spaces-to-one-space/14704571#14704571) for more versions – Déjà vu Feb 16 '13 at 17:22
0

From your earlier query, I modified the logic to fit your requirement. Hope this helps.

FILE *in;
char ch,str[100],cw;
int j,i = 0;
int isSpace = 0;

in=fopen("duom.txt","r");

if(in){
    while(!feof(in)){
    ch=getc(in);
    if(isSpace)
        isSpace = (isSpace & (ch == ' '));
    if(!isSpace) {
        str[i] = ch;
        i++;
    }
    if(ch == ' ')
        isSpace = 1;

}

for(j=0;j<i;j++){
    printf("%c",str[j]);
}
Ganesh
  • 5,880
  • 2
  • 36
  • 54
  • You're assuming the OP is performing live input of the text, but I don't think that's a valid (or useful) assumption. – mah Feb 16 '13 at 16:55
  • @mah I had referred to the previous question from the same user http://stackoverflow.com/questions/14911803/how-to-read-text-from-textfile-and-store-every-character-in-an-array/14911898#14911898 . I have anyways updated the entire program – Ganesh Feb 16 '13 at 17:00
  • it helped, thanks but could you explain this line: isSpace = (isSpace & (ch == ' ')); ? – Lukas M. Feb 16 '13 at 17:28
  • `isSpace` is set when we read a first space. However, I need to reset the flag the moment we encounter the first non-space character after a series of spaces. Thanks for accepting the answer – Ganesh Feb 16 '13 at 23:19
0

isspace from <ctype.h> could be useful here. Here is a possible implementation:

void delete_multiple_spaces(char *dst, const char *src)
{
    int previous = 0;
    int c;

    while ((c = *src++) != '\0')
    {
        if (isspace(c) && !previous)
        {
            previous = 1;
        }
        else
        {
            if (previous)
            {
                *dst++ = ' ';
                previous = 0;
            }

            *dst++ = c;
        }
    }

    *dst = '\0';
}
md5
  • 23,373
  • 3
  • 44
  • 93
0
   j = 0;
   for(i=0; myStr[i] != '\0'; i++) {
      if(myStr[i] == ' ' && myStr[i+1] == ' ') 
          continue; 
      newStr[j] = myStr[i];
      j++;
   }

And don't forget to add '\0' (Which indicates the end of the string) to the end of newStr

Maroun
  • 94,125
  • 30
  • 188
  • 241
0
char *(strdupcompact) (const char *c)
{
 int i; int p;


 for (i = 0, p = 0; c[i]; i++, p++)
 {
  if (c[i] == ' ') while (c[i+1] == ' ') i++;
 }

 char *newstr = malloc(p + 1);


 for (i = 0, p = 0; c[i]; i++, p++)
 {
  newstr[p] = c[i];
  if (c[i] == ' ') while (c[i+1] == ' ') i++;
 }
 newstr[p] = 0;

 return newstr;
}

Makes a malloced copy of your string.

Exaeta
  • 300
  • 1
  • 4