473,405 Members | 2,210 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,405 software developers and data experts.

K&R exercise 5-5

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

}

Feb 20 '07 #1
10 1506
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;

Feb 20 '07 #2
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.netwrote:
/***

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

}

Feb 20 '07 #3
mdh
On Feb 19, 8:31 pm, "Klarth" <kah....@gmail.comwrote:
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.

Feb 20 '07 #4
mdh
On Feb 19, 8:25 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
This while loop should be modified as follows:

while (*u)
++u;


thank you.

Feb 20 '07 #5
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>
Feb 20 '07 #6
On Feb 19, 11:02 pm, "mdh" <m...@comcast.netwrote:
/***

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

Feb 20 '07 #7
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.
>
}
Feb 20 '07 #8
On 20 Feb 2007 13:04:20 -0800, "DanielJohnson" <di********@gmail.com>
wrote in comp.lang.c:
On Feb 19, 11:02 pm, "mdh" <m...@comcast.netwrote:
/***

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
Feb 21 '07 #9
mdh
On Feb 20, 1:08 pm, "santosh" <santosh....@gmail.comwrote:
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.
Feb 21 '07 #10
On 20 Feb, 21:08, "santosh" <santosh....@gmail.comwrote:
mdh wrote:
....
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.
I agree entirely! To shut up gcc I renamed them to "mystr..."
int i=5;

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...
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.
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);
....
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.
An ugly suggestion, IMHO. I prefer the suggestion of

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

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.
*u='\0';

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.

Feb 21 '07 #11

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

Similar topics

6
by: leonard greeff | last post by:
I want to know the correct way to answer exercise 1-11 of K&R. The only bug that I can find is that nw counts one to many words. (if there are 8 words, nw will be 9) Am I correct aor is there more...
46
by: Herrcho | last post by:
Hi~ i've studied C for a few months myself, and i'd appreciate it if anyone could improve my coding or correct it. the following is my solution to the K&R exercise 2-3 "Write the function...
12
by: Chris Readle | last post by:
Ok, I've just recently finished a beginning C class and now I'm working through K&R2 (alongside the C99 standard) to *really* learn C. So anyway, I'm working on an exercise in chapter one which...
12
by: Merrill & Michele | last post by:
It's very difficult to do an exercise with elementary tools. It took me about fifteen minutes to get exercise 1-7: #include <stdio.h> int main(int orange, char **apple) { int c; c=-5;...
8
by: Mike S | last post by:
Hi all, I noticed a very slight logic error in the solution to K&R Exercise 1-22 on the the CLC-Wiki, located at http://www.clc-wiki.net/wiki/KR2_Exercise_1-22 The exercise reads as...
16
by: Josh Zenker | last post by:
This is my attempt at exercise 1-10 in K&R2. The code looks sloppy to me. Is there a more elegant way to do this? #include <stdio.h> /* copies input to output, printing */ /* series of...
19
by: arnuld | last post by:
this programme runs without any error but it does not do what i want it to do: ------------- PROGRAMME -------------- /* K&R2, section 1.6 Arrays; Exercise 1-13. STATEMENT: Write a program...
5
by: arnuld | last post by:
this is a programme that counts the "lengths" of each word and then prints that many of stars(*) on the output . it is a modified form of K&R2 exercise 1-13. the programme runs without any...
9
by: JFS | last post by:
I know most of you have probably read "The C Programming Language" (K&R) at some point. Well here is something that is driving me crazy. The exercises are impossible (most of them) for me to do....
88
by: santosh | last post by:
Hello all, In K&R2 one exercise asks the reader to compute and print the limits for the basic integer types. This is trivial for unsigned types. But is it possible for signed types without...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.