Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > K&R exercise 5-5

Reply
Thread Tools

K&R exercise 5-5

 
 
mdh
Guest
Posts: n/a
 
      02-20-2007
/***

I wonder if anyone can see why this is not working as planned
( concatenate n characters from t[] to s[]). I copy s[] to u[], then
add n characters to []u from t[]. When I debug, I see that u[] does
have the correct string, but when printed, it does not do so.
Thank you.
code below.


******/





#include <stdio.h>
# define SIZE 1000

int main () {

void strncat( char *s, char *t, int );
void strcpy(char *s,char *u);

int i=5;
char s[]="Now is the time for all good men";
char t[]="To be counted";
char u[SIZE];

strcpy(u, s);
strncat(u, t, i);



printf( "To the phrase: \"%s\"\n is added the first %d letters of the
phrase \"%s\"\n resulting in phrase: \"%s\" \n\n", s, i ,t, u);
return 0;
}


void strcpy(char *u, char *s){

while ( *u++ = *s++);

}

void strncat( char *u, char *t, int i){

while ( *u++);

while ( (*u++ = *t++) && i-- > 0 );

*u='\0';

}

 
Reply With Quote
 
 
 
 
subramanian100in@yahoo.com, India
Guest
Posts: n/a
 
      02-20-2007
> void strncat( char *u, char *t, int i){
>
> while ( *u++);
>


Here you are going one character after the '\0' character.
This while loop should be modified as follows:

while (*u)
++u;

 
Reply With Quote
 
 
 
 
Klarth
Guest
Posts: n/a
 
      02-20-2007
I'm not 100% sure why you are redefining strcpy and strncat, but that
is another matter. In strncat, your first while loop is searching for
end of the string u, which is null terminated. The problem is that
when the while loop terminates, it the u pointer has gone pass the
original terminating null.


On Feb 20, 1:02 pm, "mdh" <m...@comcast.net> wrote:
> /***
>
> I wonder if anyone can see why this is not working as planned
> ( concatenate n characters from t[] to s[]). I copy s[] to u[], then
> add n characters to []u from t[]. When I debug, I see that u[] does
> have the correct string, but when printed, it does not do so.
> Thank you.
> code below.
>
> ******/
>
> #include <stdio.h>
> # define SIZE 1000
>
> int main () {
>
> void strncat( char *s, char *t, int );
> void strcpy(char *s,char *u);
>
> int i=5;
> char s[]="Now is the time for all good men";
> char t[]="To be counted";
> char u[SIZE];
>
> strcpy(u, s);
> strncat(u, t, i);
>
> printf( "To the phrase: \"%s\"\n is added the first %d letters of the
> phrase \"%s\"\n resulting in phrase: \"%s\" \n\n", s, i ,t, u);
> return 0;
>
> }
>
> void strcpy(char *u, char *s){
>
> while ( *u++ = *s++);
>
> }
>
> void strncat( char *u, char *t, int i){
>
> while ( *u++);
>
> while ( (*u++ = *t++) && i-- > 0 );
>
> *u='\0';
>
> }



 
Reply With Quote
 
mdh
Guest
Posts: n/a
 
      02-20-2007
On Feb 19, 8:31 pm, "Klarth" <kah....@gmail.com> wrote:
> I'm not 100% sure why you are redefining strcpy and strncat, .......


It's an exercise in K&R II


> In strncat, your first while loop is searching for
> end of the string u, which is null terminated. The problem is .......the u pointer has gone pass the
> original terminating null.
>



Of course...it was there all the time, but I did not see the problem.

Thank you.

 
Reply With Quote
 
mdh
Guest
Posts: n/a
 
      02-20-2007
On Feb 19, 8:25 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.com> wrote:
> This while loop should be modified as follows:
>
> while (*u)
> ++u;




thank you.

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      02-20-2007
Klarth wrote:

> I'm not 100% sure why you are redefining strcpy and strncat



Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>
 
Reply With Quote
 
DanielJohnson
Guest
Posts: n/a
 
      02-20-2007
On Feb 19, 11:02 pm, "mdh" <m...@comcast.net> wrote:
> /***
>
> I wonder if anyone can see why this is not working as planned
> ( concatenate n characters from t[] to s[]). I copy s[] to u[], then
> add n characters to []u from t[]. When I debug, I see that u[] does
> have the correct string, but when printed, it does not do so.
> Thank you.
> code below.
>
> ******/
>
> #include <stdio.h>
> # define SIZE 1000
>
> int main () {
>
> void strncat( char *s, char *t, int );
> void strcpy(char *s,char *u);
>
> int i=5;
> char s[]="Now is the time for all good men";
> char t[]="To be counted";
> char u[SIZE];
>
> strcpy(u, s);
> strncat(u, t, i);
>
> printf( "To the phrase: \"%s\"\n is added the first %d letters of the
> phrase \"%s\"\n resulting in phrase: \"%s\" \n\n", s, i ,t, u);
> return 0;
>
> }
>
> void strcpy(char *u, char *s){
>
> while ( *u++ = *s++);
>
> }
>
> void strncat( char *u, char *t, int i){
>
> while ( *u++);
>
> while ( (*u++ = *t++) && i-- > 0 );
>
> *u='\0';
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -


Please indent the code for better readability

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      02-20-2007
mdh wrote:
> /***
> I wonder if anyone can see why this is not working as planned
> ( concatenate n characters from t[] to s[]). I copy s[] to u[], then
> add n characters to []u from t[]. When I debug, I see that u[] does
> have the correct string, but when printed, it does not do so.
> Thank you.
> code below.
> ******/
>
> #include <stdio.h>
> # define SIZE 1000
>
> int main () {
>
> void strncat( char *s, char *t, int );
> void strcpy(char *s,char *u);


It's not a good idea to redine Standard library functions. Give them a
name from the user's namespace.

> int i=5;


Why hard code this value. Just get the length of the second string
from strlen and use that.

> char s[]="Now is the time for all good men";
> char t[]="To be counted";
> char u[SIZE];
>
> strcpy(u, s);
> strncat(u, t, i);
>
> printf( "To the phrase: \"%s\"\n is added the first %d letters of the
> phrase \"%s\"\n resulting in phrase: \"%s\" \n\n", s, i ,t, u);


You can't split a string like that with a direct newline. You need to
close the previous string and begin a new string literal on the second
line. In C, adjacent string literals are concactenated.

Do it like:

printf( "To the phrase: \"%s\"\n is added the first %d letters of
the "
"phrase \"%s\"\n resulting in phrase: \"%s\" \n\n", s, i ,t,
u);

> return 0;
> }
>
>
> void strcpy(char *u, char *s){
>
> while ( *u++ = *s++);
>
> }
>
> void strncat( char *u, char *t, int i){
>
> while ( *u++);


When this loop terminates u points to one past the terminating null
character of the string. String concatenation functions must remove
the original null character and place at the end of the expanded
string. Here you don't do that, so printf will stop printing the
string at this point. Just add a u--; statement below the while loop.

> while ( (*u++ = *t++) && i-- > 0 );


You don't need the comparision with 0. Change i to an unsigned integer
type and it's increment in the above loop to the prefix form.

>
> *u='\0';


This is not needed.

>
> }


 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      02-21-2007
On 20 Feb 2007 13:04:20 -0800, "DanielJohnson" <>
wrote in comp.lang.c:

> On Feb 19, 11:02 pm, "mdh" <m...@comcast.net> wrote:
> > /***
> >
> > I wonder if anyone can see why this is not working as planned
> > ( concatenate n characters from t[] to s[]). I copy s[] to u[], then
> > add n characters to []u from t[]. When I debug, I see that u[] does
> > have the correct string, but when printed, it does not do so.
> > Thank you.
> > code below.
> >
> > ******/
> >
> > #include <stdio.h>
> > # define SIZE 1000
> >
> > int main () {
> >
> > void strncat( char *s, char *t, int );
> > void strcpy(char *s,char *u);
> >
> > int i=5;
> > char s[]="Now is the time for all good men";
> > char t[]="To be counted";
> > char u[SIZE];
> >
> > strcpy(u, s);
> > strncat(u, t, i);
> >
> > printf( "To the phrase: \"%s\"\n is added the first %d letters of the
> > phrase \"%s\"\n resulting in phrase: \"%s\" \n\n", s, i ,t, u);
> > return 0;
> >
> > }
> >
> > void strcpy(char *u, char *s){
> >
> > while ( *u++ = *s++);
> >
> > }
> >
> > void strncat( char *u, char *t, int i){
> >
> > while ( *u++);
> >
> > while ( (*u++ = *t++) && i-- > 0 );
> >
> > *u='\0';
> >
> >
> >
> > }- Hide quoted text -
> >
> > - Show quoted text -

>
> Please indent the code for better readability


Please learn how to post without appending those stupid strings about
"quoted text" and making them appear to be part of the post you are
replying to when they are not.

If you can't figure out how to use Google groups properly, either get
a real newsreader to post with, or don't post.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
mdh
Guest
Posts: n/a
 
      02-21-2007
On Feb 20, 1:08 pm, "santosh" <santosh....@gmail.com> wrote:
> You don't need the comparision with 0. Change i to an unsigned integer
> type and it's increment in the above loop to the prefix form.


Thanks for all your input santosh. I have just "discovered" the
usefullness of the comparison to zero. Finally just starting to enjoy
C...very much in part to the great help provided by this forum.


 
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
tree functions daily exercise: Range Xah Lee Java 12 06-22-2005 08:51 AM
Cisco Student VPN exercise problem : gen_unrfrag: fail to generate unreachable, unexpected args robert Cisco 0 06-02-2004 07:33 PM
2154 module 4 Exercise 2 Drew Brown MCSE 0 10-22-2003 02:47 AM
Exercise needed for java 2 programmer test lonelyplanet999 Java 1 09-30-2003 10:37 AM
Re: Development best practices and knowing when to exercise control over development Kevin Spencer ASP .Net 2 08-06-2003 09:33 PM



Advertisments