LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   problems in removing white spaces from string of text (https://www.linuxquestions.org/questions/programming-9/problems-in-removing-white-spaces-from-string-of-text-299061/)

monil 03-08-2005 05:23 AM

problems in removing white spaces from string of text
 
Hi,

I need to write a program that shall strip all white spaces from a given string with just 1 space after each word.

Eg. If I give the following string
Code:

  rm        asd.txt  tmp.txt                hgfh.txt
Note: There are training spaces in beginnign and the end...

It should return me
Code:

rm asd.txt tmp.txt hgfh.txt
Also, After the tring is formatted properly, i need to pair up the 1st and the next word.

Like
Code:

rm asd.txt
rm tmp.txt
rm hgfh.txt

Heres the code that i am using...
Code:

#include <stdio.h>
#include <string.h>

int main()
{
  char tmp[100], cmd[100], arg[100];
  int cnt=0, spc_cnt=0, i;

  for(cnt=0; cnt < 100; cnt++)
{
    tmp[cnt] = NULL;
    cmd[cnt] = NULL;
    arg[cnt] = NULL;
}

printf("Please enter some text... ");
gets(tmp);

i=0;
for(cnt=0; ( cnt<100 ) || (tmp[cnt] == NULL); cnt++)
{
        if( tmp[cnt] == ' ' )
        {
                ;
        }
        else
        {
                for( ; (cnt<100) || (tmp[cnt] == NULL); cnt++, i++)
                {
                        if( tmp[cnt] != ' ' )
                        {
                                cmd[i] = tmp[cnt];
                        }
                        else
                        {
                                cmd[i] = ' ';
                                cnt++; i++;
                                break;
                        }
                }
        }
}
  printf("TMP: %s\n", tmp);
  printf("CMD: %s\n", cmd);
  printf("ARG: %s\n", arg);
  return 0;
}

Heres the output I get...

Please enter some text... asd fghfgh rtytr
TMP: asd fghfgh rtytr
CMD: asd ghfgh tytr
ARG:

in CMD, second argument , f is missing... and in third argument, r is missing.

Please help me get away from this error..

Thank you.

-Monil

Mara 03-08-2005 05:46 AM

Re: problems in removing white spaces from string of text
 
The problem is here:
Code:

for( ; (cnt<100) || (tmp[cnt] == NULL); cnt++, i++)
{
    if( tmp[cnt] != ' ' )
    {
        cmd[i] = tmp[cnt];
    }
    else
    {
        cmd[i] = ' ';
        cnt++; i++;
        break;
      }
}

When there's a space, both positions are adjusted, but then, in the loop, they're adjusted again. That's why you miss the characters.

My correction (on of the possiblilties, not tested):
Code:

for( ; (cnt<100) || (tmp[cnt] == NULL); cnt++)
{
    if( tmp[cnt] != ' ' )
    {
        cmd[i] = tmp[cnt];
        i++;
    }
    else
    {
        cmd[i] = ' ';
        cnt++;
        break;
      }
}


monil 03-08-2005 06:01 AM

Hi,

Thnx for your quick reply..

But, now, I get the following

Code:

bash-2.04$ ./a.out
Please enter some text... sdf ertyrt yrty
TMP:
CMD: s
ARG:

Its not even displaying the string that i had taken as input...

Can you please throw some light on this?

Thank you again for your help...

-Monil

monil 03-08-2005 09:37 AM

Input: rm a b c

I need to split it as

Code:

rm a
rm b
rm c

Well, the number of arguments is variable.. Any idea how can i achieve thjis?

Thank you.

-Monil

Brain Drop 03-08-2005 09:45 AM

Is this for a school assignment?

hk_linux 03-08-2005 09:56 AM

Why such a requirement? I hope it is not an assignment question. Perl would be a very simple option for the same. This will even squeeze the whitespace characters.

#!/usr/bin/perl -w
$a = <STDIN>;
chomp ($a);
@tok = split (" ", $a);
for ($i = 1; $i < $#tok + 1; $i ++)
{
print "$tok[0] $tok[$i]\n";
}


HTH.

monil 03-08-2005 10:11 AM

Hi,

Thank you for your reply...

Its not a school assignment. With my team, I am working to simulate the *NIX file system using C.

We have this requirement and we are not getting a good solution to implement the same other than the one I have mentioned earlier.

I would appreciate if you could guide us in getting this solved...

Thank you.

-Monil

keefaz 03-08-2005 11:28 AM

This code works, but I think it needs improvement
I used fgets after reading the man page for gets
If you use windows, maybe replace the "\n" with '\r\n' in the condition test
Code:

#include <stdio.h>

#define MAX_ARGS            8
#define MAX_ARGS_CHARS    128
#define MAX_COMMAND_CHARS 256

int main() {
    char buffer[MAX_COMMAND_CHARS];
    char arguments[MAX_ARGS][MAX_ARGS_CHARS];

    int i,j = 0;
    int count = 0;
    char c;


    printf("Enter string: ");
    fgets(buffer, MAX_COMMAND_CHARS -1, stdin);

    for(i=0; i < MAX_COMMAND_CHARS; i++) {
        c = buffer[i];

        if(c > 32 && j < MAX_ARGS_CHARS) {
            arguments[count][j++] = c;

        } else {
            if(!i && c == '\n') {
                printf("The string was blank\n");
                return 1;
            }

            if(j) {
                arguments[count][j] = '\0';
                j = 0;
                count++;
                if(count == MAX_ARGS || c == '\n') break;
            }
        }
    }

    if(count < 2) {
        printf("You entered only one string\n");
        return 1;
    }

    for(i = 1; i < count; i++) {
        printf("%s %s\n", arguments[0], arguments[i]);
    }
    return 0;
}



All times are GMT -5. The time now is 11:09 AM.