K& R练习5-5 [英] K&R exercise 5-5

查看:91
本文介绍了K& R练习5-5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/ ***


我想知道是否有人能够理解为什么这不按计划工作

(连接从t []到s的n个字符[])。我将s []复制到你[],然后

从t []向[] u添加n个字符。当我调试时,我看到你[]确实

有正确的字符串,但是在打印时,它没有这样做。

谢谢。

以下代码。

****** /


#include< stdio.h>

#定义SIZE 1000

int main(){


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

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


int i = 5;

char s [] ="现在是所有好人的时间;

char t [] ="待计算;

char u [SIZE];


strcpy(u,s);

strncat(u,t,i);


printf(&to To the phrase:\ %s \和\\ n添加了

短语\"%s \" \ n的前%d个字母,从而产生短语:\" %s \" \ n \ n",s,i,t,u);

返回0;

}

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


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


}


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


while(* u ++);


而((* u ++ = * t ++)&&我 - 0);


* u =''\ 0'';


}

/***

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

}

推荐答案

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

>

while(* u ++);
>
while ( *u++);



这里你要在''\ 0''后面加一个字符。

这个while循环应修改如下:


while(* u)

++ u;


我不是100%肯定你为什么要重新定义strcpy和strncat,但是

是另一回事。在strncat中,你的第一个while循环是在字符串u中搜索

结尾,它是以null结尾的。问题是

当while循环终止时,u指针已经通过

原始终止null。

2月20日,下午1:02,mdh < m ... @ comcast.netwrote:
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:

/ ***


我想知道是否有人能看出原因这不按计划工作

(连接从t []到s []的n个字符)。我将s []复制到你[],然后

从t []向[] u添加n个字符。当我调试时,我看到你[]确实

有正确的字符串,但是在打印时,它没有这样做。

谢谢。

以下代码。


****** /


#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 [] = 现在是所有好人的时间;

char t [] ="要算&;;

char u [SIZE];


strcpy(u,s);

strncat(u,t,i);


printf(&to; to to the短语:\"%s \" \ n添加了

短语\"%s \" \ n的前%d个字母,从而产生短语: \"%s \" \ n \ n",s,i,t,u);

返回0;


}


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


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


}


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


while(* u ++);


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


* u =''\ 0'';


}
/***

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

}



2月19日晚上8:31,Klarth < kah .... @ gmail.comwrote:
On Feb 19, 8:31 pm, "Klarth" <kah....@gmail.comwrote:

我不是100%肯定你为什么要重新定义strcpy和strncat,...... 。
I''m not 100% sure why you are redefining strcpy and strncat, .......



这是K& R II的练习

It''s an exercise in K&R II


在strncat中,你的第一个while循环正在搜索字符串u的结尾

,它是空终止的。问题是.......你的指针已经通过

原始终止null。
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.


这篇关于K&amp; R练习5-5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆