473,395 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

a little mistake

ash
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

Jun 16 '06
54 2910
av
On Mon, 19 Jun 2006 16:57:37 +0000, Richard Heathfield
<in*****@invalid.invalid> wrote:
CBFalconer said:
Tom St Denis wrote:

... snip ...

The fact that on basically every platform 0 == NULL is just an
added bonus.


Making that assumption, in any case where it may matter, is
extremely sloppy programming.


if(0 == NULL)
{
puts("No, it's guaranteed that 0 == NULL.");
puts("This stuff will always be written to stdout.");
}


if stdout is open
Jun 19 '06 #51
Tom St Denis wrote:
When you calloc a structure with pointers, they're VERY LIKELY to be
equal to NULL. this is the "added bonus" I was talking about.


#include <stdio.h>
#include <stdlib.h>

int main()
{
if ( rand() % 12345678 == 7654321 )
printf("tom is an egg\n");

return 0;
}

It's VERY LIKELY that this program will never offend anyone
when run. Does that make it acceptable?

Jun 20 '06 #52
On Mon, 19 Jun 2006 14:58:58 +0000, Richard Heathfield
<in*****@invalid.invalid> wrote:
The following technique has served me well when creating aggregate objects
of type T:

#include <stdlib.h>
#include "t.h"

T *T_create(int this, double that, long theother)
{
const T blank = {0};
I declare this static. Although a good enough compiler could figure
that for me, it's easy enough not to assume/rely on that.
T *new = malloc(sizeof *new);
if(new != NULL)
{
*new = blank; /* get default static initialisers in place */
new->this = this;
new->that = that;
new->theother = theother; /* override specific fields */

/* all other fields in 'new' have their default values */
}
return new;
}

This way guarantees that *either* the allocation fails (in which case 'new'
is NULL) *or* it succeeds and no member object of 'new' will be
uninitialised.


Alternatively, I might soon start using C99 *new = {.this = etc.} .
- David.Thompson1 at worldnet.att.net
Jun 26 '06 #53
Dave Thompson posted:
On Mon, 19 Jun 2006 14:58:58 +0000, Richard Heathfield
<in*****@invalid.invalid> wrote:
The following technique has served me well when creating aggregate
objects of type T:

#include <stdlib.h>
#include "t.h"

T *T_create(int this, double that, long theother)
{
const T blank = {0};

I declare this static. Although a good enough compiler could figure
that for me, it's easy enough not to assume/rely on that.

I would think that that would be misuse of the "static" keyword.

The purpose of static variables within functions is that we can retain a
value until the next invocation of that function.
--

Frederick Gotham
Jun 26 '06 #54
Frederick Gotham wrote:
Dave Thompson posted:
On Mon, 19 Jun 2006 14:58:58 +0000, Richard Heathfield
<in*****@invalid.invalid> wrote:
The following technique has served me well when creating aggregate
objects of type T:

#include <stdlib.h>
#include "t.h"

T *T_create(int this, double that, long theother)
{
const T blank = {0};
I declare this static. Although a good enough compiler could figure
that for me, it's easy enough not to assume/rely on that.


I would think that that would be misuse of the "static" keyword.


Why? `static` says there's just the one `blank`, rather than a new
`blank` each function call. That seems to be just fine.
The purpose of static variables within functions is that we can retain a
value until the next invocation of that function.


That's not /the/ purpose. That's /a/ purpose. Another purpose is to
hide a constant from sibling functions.

--
Chris "dynamic" Dollin
"People are part of the design. It's dangerous to forget that." /Star Cops/

Jun 26 '06 #55

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Henk | last post by:
Hi Guys, (see actual code below) I wrote a little program that is supposed to read a file (input.txt) into memory (using a stack) and then reverse the file and display it to output. It works,...
2
by: Jesper. | last post by:
Hi, I'm a former C++ programmer, and normally I didn't see the use of the this pointer (pointer in c++) used as often as I see it in C#. When the VS generates code for you it uses >this< all...
9
by: Eric S. | last post by:
Why doesn't something like this work? I get "a constant value is expected" on all 3 case statements. private DataGrid CurrentDataGrid(DataGrid firstDg, DataGrid secondDg) { try { switch...
14
by: tranky | last post by:
Hi, i'm italian...so...excuse me for my english. I've a little problem....in what manner i can check a textbox for know if it contain only character from A-Z (a-z), numbers (0-9), and underscore...
16
by: mdh | last post by:
Can someone look at this little program and tell me why the last line is not compiling. #include <stdio.h> int main (){ int i, j, k; i=0;
13
by: junky_fellow | last post by:
Hi guys, I need to convert a big endian integer to little endian integer. (the integer is 4 bytes in size on my implementation). I came up with the following code. I need your comments on...
4
by: Batmanuel | last post by:
Good evening people, little question here... I'm trying to get this file upload script to work but it tells me that move_uploaded_file() fails because it doesn't have permission for the /tmp...
6
by: djm | last post by:
hello everyone, im doing a c++ coursework which consists linked lists and use of classes. but im getting some compilation errors. can some please help me out. //this is the header file...
7
by: CuTe_Engineer | last post by:
hii, can you tell me plzz why my programme doesn`t work ,i don`t have any errors and every thing is fine but i don`t know why it`s not working , soo plz can you help me un finding my mistake i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.