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