LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-08-2005, 06:23 AM   #1
monil
LQ Newbie
 
Registered: Mar 2005
Posts: 19

Rep: Reputation: 0
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
 
Old 03-08-2005, 06:46 AM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,539

Rep: Reputation: 149Reputation: 149
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;
      }
}
 
Old 03-08-2005, 07:01 AM   #3
monil
LQ Newbie
 
Registered: Mar 2005
Posts: 19

Original Poster
Rep: Reputation: 0
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
 
Old 03-08-2005, 10:37 AM   #4
monil
LQ Newbie
 
Registered: Mar 2005
Posts: 19

Original Poster
Rep: Reputation: 0
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
 
Old 03-08-2005, 10:45 AM   #5
Brain Drop
Member
 
Registered: Feb 2003
Location: just outside reality
Distribution: balanced
Posts: 752

Rep: Reputation: 35
Is this for a school assignment?
 
Old 03-08-2005, 10:56 AM   #6
hk_linux
Member
 
Registered: Nov 2004
Location: India
Distribution: RedHat, PCQLinux, Fedora
Posts: 95

Rep: Reputation: 15
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.
 
Old 03-08-2005, 11:11 AM   #7
monil
LQ Newbie
 
Registered: Mar 2005
Posts: 19

Original Poster
Rep: Reputation: 0
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
 
Old 03-08-2005, 12:28 PM   #8
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,617

Rep: Reputation: 136Reputation: 136
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;
}

Last edited by keefaz; 03-08-2005 at 12:31 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to get rid of the input beginning white spaces feetyouwell Programming 3 09-30-2004 02:33 AM
Remove white spaces in HTML files using shell : Possible???????? cpanelskindepot Programming 2 08-06-2004 12:14 AM
Removing spaces from string qcoder Programming 3 07-05-2004 01:35 PM
Removing Text from a Shell String OrangeLefty Programming 2 04-19-2004 12:08 AM
spaces in a string Longinus Programming 18 03-08-2004 07:02 PM


All times are GMT -5. The time now is 03:35 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration