K&R exercise 5-5

M

mdh

/***

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';

}
 
S

subramanian100in

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;
 
K

Klarth

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.
 
M

mdh

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.
 
D

DanielJohnson

/***

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
 
S

santosh

mdh said:
/***
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.

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.

This is not needed.
 
J

Jack Klein

/***

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.
 
M

mdh

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.
 
M

mark_bluemel

mdh wrote: ....


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

I agree entirely! To shut up gcc I renamed them to "mystr..."
Why hard code this value. Just get the length of the second string
from strlen and use that.

Except he might not want the whole of the second string...
char s[]="Now is the time for all good men";
char t[]="To be counted";
char u[SIZE];

while "i" and "j" are fair enough names for numbers used as indexes
(old FORTRAN convention I believe), I think here we'd be better off
with more meaningful names... It's a good habit to get into early in
your programming life...
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.

I think that was just the newsreader, or posting software, wrapping...

Just like it does with yours below :)
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);
....



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.

An ugly suggestion, IMHO. I prefer the suggestion of

while(*u)
u++;
You don't need the comparision with 0.

True, but it doesn't actually do any harm and _may_ improve
readability.
Change i to an unsigned integer type

Any particular reason for that?
and it's increment in the above loop to the prefix form.

This is not needed.

Unless we exited the loop above due to the limit of characters copied
rather than by
copying the final '\0' in which case the result string is unterminated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top