這是我的代碼的要求:
此函式將字串附加
src到dest字串,覆寫 結尾'\0'處的終止空位元組 ( )dest,然后添加終止空位元組。回傳指向結果字串的指標
dest。
這是我得到的輸出:
Hello
World!
Hello World!
Hello
這是我的代碼:
char *_strcat(char *dest, char *src) {
int lengthd = 0;
int lengths = 0;
int i = 0;
int j = 0;
int k = 0;
char tmp[10];
while (dest[i] != '\0') {
lengthd ;
i ;
}
while (src[k] != '\0') {
tmp[lengths] = src[k];
lengths ;
k ;
}
for (; j < lengths - 1; j ) {
dest[lengthd 1] = tmp[j];
}
dest[lengthd 1] = '\0';
return (dest);
}
int main(void) {
char s1[98] = "Hello ";
char s2[] = "World!\n";
char *ptr;
printf("%s\\n", s1);
printf("%s", s2);
ptr = _strcat(s1, s2);
printf("%s", s1);
printf("%s", s2);
printf("%s", ptr);
return (0);
}
uj5u.com熱心網友回復:
您的代碼因多種原因而失敗:
- 您使用臨時陣列來制作源字串的副本:此陣列
tmp具有 10 個位元組的固定長度,如果結果字串長于 9 個位元組,則該陣列太小。否則,當您寫入超出此陣列的末尾時,您將有未定義的行為。 - 無論如何,確實不需要這個臨時陣列。
- 最后一個回圈在 停止
lengths - 1,因此您在副本的最后一個位元組之前停止。 - 您將所有位元組復制到同一位置
dest[lengthd 1] - 您終于再次將空終止符設定在同一位置。
- 中的測驗
main()無法產生您發布的輸出,可能是因為"%s\\n". - 避免使用以 . 開頭的識別符號
_。
這是修改后的版本:
#include <stdio.h>
#include <string.h>
char *my_strcat(char *dest, char *src) {
int i = 0;
int k = 0;
/* find the offset of the null terminator in dest */
while (dest[i] != '\0') {
i ;
}
/* copy the bytes from the src string there */
while (src[k] != '\0') {
dest[i] = src[k];
i ;
k ;
}
/* set the null terminator */
dest[i] = '\0';
/* return the pointer to the destination array */
return dest;
}
int main(void) {
char s1[98] = "Hello ";
char s2[] = "World!";
char *ptr;
printf("%s\n", s1);
printf("%s", s2);
ptr = my_strcat(s1, s2);
printf("%s", s1);
printf("%s", s2);
printf("%s", ptr);
return 0;
}
請注意,源字串沒有被修改,并且偏移量應該具有型別size_t并且可以作為賦值的副作用而增加:
char *my_strcat(char *dest, const char *src) {
size_t i = 0;
size_t k = 0;
/* find the offset of the null terminator in dest */
while (dest[i] != '\0') {
i ;
}
/* copy the bytes from the src string there */
while (src[k] != '\0') {
dest[i ] = src[k ];
}
/* set the null terminator */
dest[i] = '\0';
/* return the pointer to the destination array */
return dest;
}
您還可以使用指標而不是偏移量:
char *my_strcat(char *dest, const char *src) {
/* use a working pointer to preserve dest for the return value */
char *p = dest;
/* find the offset of the null terminator in dest */
while (*p != '\0') {
p ;
}
/* copy the bytes from the src string there */
while (*src != '\0') {
*p = *src ;
}
/* set the null terminator */
*p = '\0';
/* return the pointer to the destination array */
return dest;
}
最后一個更改:您可以結合讀取源位元組、復制到目標和測驗空終止符,這將已經被復制:
char *my_strcat(char *dest, const char *src) {
/* use a working pointer to preserve dest for the return value */
char *p = dest;
/* find the offset of the null terminator in dest */
while (*p != '\0') {
p ;
}
/* copy the bytes from the src string there */
while ((p = *src ) != '\0') {
/* nothing */
}
/* the null terminator was copied from the source string */
/* return the pointer to the destination array */
return dest;
}
uj5u.com熱心網友回復:
至少由于tmp用幻數宣告了字符陣列10
char tmp[10];
該功能沒有意義。
此外在這個while回圈中
while (src[k] != '\0')
{
lengths ;
k ;
tmp[lengths] = src[k];
}
tmp跳過陣列的第一個元素。
同樣在這個for回圈中
for (; j < lengths-1; j )
{
dest[lengthd 1] = tmp[j];
}
回圈的條件不正確。該運算式還dest[lengthd 1]跳過了指標指向的字串的終止零字符dest。并且所有字符都寫在同一個位置lengthd 1。
除此之外,未宣告名稱s1和s2mainptr中使用的名稱。
該函式可以通過以下方式宣告和定義
char * my_strcat( char *dest, const char *src )
{
char *p = dest;
while ( *p ) p;
while ( ( *p = *src ) != '\0' );
return dest;
}
并且可以稱為
char s1[13] = "Hello ";
const char *s2 = "World!";
puts( my_strcat( s1, s2 ) );
使用類似于您的方法定義函式的另一種方法如下
char * my_strcat( char *dest, const char *src )
{
size_t i = 0;
while ( dest[i] != '\0' ) i;
for ( size_t j = 0; src[j] != '\0'; j )
{
dest[i ] = src[j];
}
dest[i] '\0';
return dest;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/448213.html
下一篇:正在修改陣列
