Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > a little mistake

Reply
Thread Tools

a little mistake

 
 
ash
Guest
Posts: n/a
 
      06-16-2006
i am writing this program (for exercise1-9 in k&r-2nd edition) which
removes extra spaces in string
example- "test string" will be "test string"

#include<stdio.h>
#include<string.h>
#include<conio.h>
main()
{
char str[20],st[20];
int i=0,j=0;
printf("enter string");
gets(str);

while(str[i]!=NULL)
{
if(str[i]==' ')
{ st[j++]=st[i++];

while(str[i]==' ') i++;

}
else
st[j++]=str[i++];
}
st[j]=NULL;
strcpy(str,st);
puts(str);
}

(compiled in turbo c++ for windows version 3.1)
i know there should be other ways to write this program (thats a bad
program ),can someone help me in writing that program in compact way
and give me some logic and tips.
i want to use gcc but i don`t have linux operating system and one
software which allows gcc compiling in windows (cygwin) is not working.
anyone can advice me such a program that allows compiling with gcc in
windows( this is off topic but advices are always welcome).

thankx

 
Reply With Quote
 
 
 
 
Malcolm
Guest
Posts: n/a
 
      06-16-2006



"ash" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>i am writing this program (for exercise1-9 in k&r-2nd edition) which
> removes extra spaces in string
> example- "test string" will be "test string"
>
> #include<stdio.h>
> #include<string.h>
> #include<conio.h>
> main()
> {
> char str[20],st[20];
> int i=0,j=0;
> printf("enter string");
> gets(str);
>
> while(str[i]!=NULL)
> {
> if(str[i]==' ')
> { st[j++]=st[i++];
>
> while(str[i]==' ') i++;
>
> }
> else
> st[j++]=str[i++];
> }
> st[j]=NULL;
> strcpy(str,st);
> puts(str);
> }
>
> (compiled in turbo c++ for windows version 3.1)
> i know there should be other ways to write this program (thats a bad
> program ),can someone help me in writing that program in compact way
> and give me some logic and tips.
> i want to use gcc but i don`t have linux operating system and one
> software which allows gcc compiling in windows (cygwin) is not working.
> anyone can advice me such a program that allows compiling with gcc in
> windows( this is off topic but advices are always welcome).
>
> thankx
>

Separate out ewhat you are doing into a function.

In this case you want

/*
turns whitespace spans into single spaces
(and trims leading / trailing white space?)
*/
void onespace(char *out, char *in)

Decide if you want to allow out and in to be the same. This will make your
function
more convenient to use, but you might have to be a little bit more careful
in coding it.

Basically you maintain two pointer, a "write" pointer and a "read" pointer.
If you hit
a non-whitespace you write to the "write" pointer and increment both.
If you hit a whitespace character, you write a single space to the "write"
pointer. Then you increment the "read" pointer whilst it is still pointing
to white space.
--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $7.20 paper, available www.lulu.com/bgy1mm


 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      06-16-2006
ash wrote:
.... snip ...
> i want to use gcc but i don`t have linux operating system and one
> software which allows gcc compiling in windows (cygwin) is not working.
> anyone can advice me such a program that allows compiling with gcc in
> windows( this is off topic but advices are always welcome).


Generally Cygwin is a bit complex to set up and use. Use MinGW instead.
It uses native Win32 ports of the GNU toolchain.

http://www.mingw.org/

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-16-2006
"ash" <(E-Mail Removed)> writes:
> i am writing this program (for exercise1-9 in k&r-2nd edition) which
> removes extra spaces in string
> example- "test string" will be "test string"
>
> #include<stdio.h>
> #include<string.h>
> #include<conio.h>


<conio.h> is not a standard header. You don't use it anyway. Delete
the above line.

I find it clearer to leave a space between "include" and "<".

> main()


int main(void)

> {
> char str[20],st[20];
> int i=0,j=0;
> printf("enter string");


Since this doesn't end in a newline, it's not guaranteed to appear.
Add "fflush(stdout);".

I'd add a blank, or perhaps ": ", at the end of the prompt; it looks
better. Without it, if I type "foo", I see "enter stringfoo" on the
screen.

> gets(str);


Never use gets(). Never use gets(). Never use gets().

See question 12.23 in the comp.lang.c FAQ, <http://www.c-faq.com/>.

> while(str[i]!=NULL)


NULL is a null pointer constant. You're trying to compare a character
value to a pointer value. It happens to work on your system for
obscure reasons I won't get into, but it's bad style at the very
least. The way to represent a null character is '\0'.

> {
> if(str[i]==' ')
> { st[j++]=st[i++];


I think you mean "st[j++]=st[i++];"

>
> while(str[i]==' ') i++;
>
> }
> else
> st[j++]=str[i++];
> }
> st[j]=NULL;


Again, use '\0', not NULL.

> strcpy(str,st);
> puts(str);


return 0;

> }


And don't underestimate the importance of consistent formatting,
particularly indentation.

Apart from that, the program doesn't seem unreasonable.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      06-16-2006
ash wrote:

> i am writing this program (for exercise1-9 in k&r-2nd edition) which
> removes extra spaces in string
> example- "test string" will be "test string"


That's not the actual exercise:

Exercise 1-9. Write a program to copy its input to its output,
replacing each string of one or more blanks by a single blank.


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


<conio.h> is not a standard header. You should not use it, as it's not
very portable.

> main()


Use the following form:

int main(void)

> {
> char str[20],st[20];


These fixed buffers are likely a problem.

> int i=0,j=0;
> printf("enter string");
> gets(str);


Never, ever use gets(). It's dangerous. What happens when someone
enters a string with 50 characters? Bad things.

>
> while(str[i]!=NULL)


NULL is a null pointer constant. It might be 0, which would work, but
it could be (void*)0. Either use '\0' or 0 in checking for the null
terminator in a string.

> {
> if(str[i]==' ')
> { st[j++]=st[i++];
>
> while(str[i]==' ') i++;
>
> }
> else
> st[j++]=str[i++];
> }
> st[j]=NULL;
> strcpy(str,st);
> puts(str);
> }
>
> (compiled in turbo c++ for windows version 3.1)
> i know there should be other ways to write this program (thats a bad
> program ),can someone help me in writing that program in compact way
> and give me some logic and tips.
> i want to use gcc but i don`t have linux operating system and one
> software which allows gcc compiling in windows (cygwin) is not
> working. anyone can advice me such a program that allows compiling
> with gcc in windows( this is off topic but advices are always
> welcome).


Do you want the program that K&R assigned, or the one you tried? It can
be made much more simple by just immediately outputting characters
instead of copying to buffers.

/* algorithm portion */

int c;
int flag = 0;

while ((c = getchar()) != EOF)
{
if (c == ' ')
{
if (flag == 0)
{
putchar(c);
flag = 1;
}
}
else
{
if (flag == 1)
{
flag = 0;
}
putchar(c);
}
}





Brian
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      06-16-2006
ash wrote:
>

.... snip ...
>
> i want to use gcc but i don`t have linux operating system and one
> software which allows gcc compiling in windows (cygwin) is not
> working. anyone can advice me such a program that allows compiling
> with gcc in windows( this is off topic but advices are always
> welcome).


djgpp, at <http://www.delorie.com>. However cygwin should work.

--
"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews

 
Reply With Quote
 
Julian V. Noble
Guest
Posts: n/a
 
      06-17-2006
Malcolm wrote:
> "ash" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>> i am writing this program (for exercise1-9 in k&r-2nd edition) which
>> removes extra spaces in string
>> example- "test string" will be "test string"
>>
>> #include<stdio.h>
>> #include<string.h>
>> #include<conio.h>
>> main()
>> {
>> char str[20],st[20];
>> int i=0,j=0;
>> printf("enter string");
>> gets(str);
>>
>> while(str[i]!=NULL)
>> {
>> if(str[i]==' ')
>> { st[j++]=st[i++];
>>
>> while(str[i]==' ') i++;
>>
>> }
>> else
>> st[j++]=str[i++];
>> }
>> st[j]=NULL;
>> strcpy(str,st);
>> puts(str);
>> }
>>
>> (compiled in turbo c++ for windows version 3.1)
>> i know there should be other ways to write this program (thats a bad
>> program ),can someone help me in writing that program in compact way
>> and give me some logic and tips.
>> i want to use gcc but i don`t have linux operating system and one
>> software which allows gcc compiling in windows (cygwin) is not working.
>> anyone can advice me such a program that allows compiling with gcc in
>> windows( this is off topic but advices are always welcome).
>>
>> thankx
>>

> Separate out ewhat you are doing into a function.
>
> In this case you want
>
> /*
> turns whitespace spans into single spaces
> (and trims leading / trailing white space?)
> */
> void onespace(char *out, char *in)
>
> Decide if you want to allow out and in to be the same. This will make your
> function
> more convenient to use, but you might have to be a little bit more careful
> in coding it.
>
> Basically you maintain two pointer, a "write" pointer and a "read" pointer.
> If you hit
> a non-whitespace you write to the "write" pointer and increment both.
> If you hit a whitespace character, you write a single space to the "write"
> pointer. Then you increment the "read" pointer whilst it is still pointing
> to white space.


I think you also need a little state machine: if you have not hit
a blank you are in character-accepting mode and write that char
to the buffer where you construct the compacted string. Otherwise
if the char is a blank you accept it but shift to a mode where you
no longer accept blank characters. When you hit a non-blank you
switch back to the first mode. In other words,

state\input -> non-blank blank
// --------------------------------------------------------
1 write to buff write to buff and
set state to 2

2 write to buff and drop char
set state to 1
// --------------------------------------------------------

A switch statement and a state variable will accomplish this.


--
Julian V. Noble
Professor Emeritus of Physics
University of Virginia
 
Reply With Quote
 
lovecreatesbeauty
Guest
Posts: n/a
 
      06-17-2006
ash wrote:
> can someone help me in writing that program in compact way
> and give me some logic and tips.



I don't know whether my program is compact or not, anyway code below
it's my own. Suggestion welcomed.


/* Removes extra spaces in string pointed by src, the result string is
pointed by dst. Return value is the length os the result string or
0 for failure. example: "test string" will be "test string" */
int spacerm(char *dst, char *src)
{
int last_space = 0; /*1: last char is a space char; 0: not*/
int len = 0;
int ret = 1; /*for return value, 0: failure, non-zero: result
string length returned*/

if (src == 0 || dst == 0)
{
ret = 0;
}

if (ret != 0)
{
while (*src != '\0')
{
if (*src == ' ')
{
if (last_space != 1)
{
*dst++ = *src++;
len++;
}
else
{
src++;
}

last_space = 1;

}
else
{
*dst++ = *src++;
len++;
last_space = 0;
}
}

ret = len;
}

return ret;
}

#include <stdio.h>
#include <string.h>
int main(void)
{
char *s = "test string";
char a[20] = {'\0'};
int i = 0;

printf("%i, %s\n", strlen(s), s);
i = spacerm(a, s);
printf("%i, %s\n", i, a);

return 0;
}

$ gcc -W -Wall -pedantic -ansi test.c
$ ./a.out
14, test string
11, test string
$


> i want to use gcc but i don`t have linux operating system and one
> software which allows gcc compiling in windows (cygwin) is not working.
> anyone can advice me such a program that allows compiling with gcc in
> windows( this is off topic but advices are always welcome).



Use mingw or Install a Debian Linux on VMware hosted in Windows.

lovecreatesbeauty

 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      06-17-2006
lovecreatesbeauty wrote:
>
> /* Removes extra spaces in string pointed by src, the result string is
> pointed by dst. Return value is the length os the result string or
> 0 for failure. example: "test string" will be "test string" */


What if the original string is "". Is that an error? If so, why?

> int spacerm(char *dst, char *src)
> {
> int last_space = 0; /*1: last char is a space char; 0: not*/
> int len = 0;
> int ret = 1; /*for return value, 0: failure, non-zero: result
> string length returned*/


You effectively have two variables for the same thing. This
looks clumsy.

> if (src == 0 || dst == 0)


Why bother? [Rehtorical]

> {
> ret = 0;
> }
>
> if (ret != 0)
> {
> while (*src != '\0')
> {
> if (*src == ' ')
> {
> if (last_space != 1)


if (last_space == 0)

Apart from being more efficient on many systems, it's
obvious what the test is. When you test explicitly against
a value like 1, the reader may pause to ponder whether
the variable can other values like 2 or 3 and why 1 is
significant.

> {
> *dst++ = *src++;
> len++;
> }
> else
> {
> src++;
> }
>
> last_space = 1;
>
> }
> else
> {
> *dst++ = *src++;
> len++;
> last_space = 0;
> }
> }


You never write a terminating null byte to dst.

> ret = len;
> }
>
> return ret;
> }
>
> #include <stdio.h>
> #include <string.h>
> int main(void)
> {
> char *s = "test string";
> char a[20] = {'\0'};


Try...

char a[20] = "xxxxxxxxxxxWhat?";

> int i = 0;
>
> printf("%i, %s\n", strlen(s), s);


strlen() returns a size_t value; %i expects an int.

In C99 use %zu to print a size_t. In C90, cast the value
to suitable unsigned type and use %u with an appropriate
length modifier.

> i = spacerm(a, s);
> printf("%i, %s\n", i, a);
>
> return 0;
> }
>
> $ gcc -W -Wall -pedantic -ansi test.c


Not a blip eg? Safe as houses then.

As some non-expert whose comments aren't worth the
em waves they're transmitted in once said "You control
the portability of your code, not gcc." <g>

> $ ./a.out
> 14, test string
> 11, test string
> $


--
Peter

 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      06-17-2006
ash posted:

> i am writing this program (for exercise1-9 in k&r-2nd edition) which
> removes extra spaces in string
> example- "test string" will be "test string"



I'd probably approach it something like:

#include <cstddef>

void RetreatStrXPlaces(register char *p, std::size_t const places)
{
/* Consider coding this by copying int's
and checking if any byte is a space
character */

if ( !places ) return;

register char *q = p - places;

while ( *q++ = *p++ );
}

void FixMultipleSpaces( char * const p_start )
{
for(char *p = p_start; ; )
{
switch( *p++ )
{
case 0: return;

case ' ':
{
unsigned places = 0;

while( *p++ == ' ' ) ++places;

RetreatStrXPlaces(--p, places);
}
}
}
}

#include <iostream>
#include <cstdlib>

int main()
{
using std::cout;

char buffer[] = "The man walked over the revine";

FixMultipleSpaces(buffer);

cout << buffer << '\n';

std::system("PAUSE");
}





--

Frederick Gotham
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
1 little 2 little 3 little Kennedys dale Digital Photography 0 03-23-2008 01:03 PM
having a little problem with some code for a little game I am creating. ThaDoctor C++ 3 09-28-2007 03:28 PM
New computer wireless PCI adapter setup mistake recovery =?Utf-8?B?Q3VyaW91c01hcms=?= Wireless Networking 3 02-15-2005 04:01 PM
little red X in little white box Puzzled Computer Support 8 12-13-2004 09:11 AM
where is the mistake? \(beta-\) Frank Nitzsche VHDL 4 06-25-2004 06:53 PM



Advertisments