[英]C program copy string without using strcpy() with enough memory
我想嘗試這個輸出:
Comparing results of concat and strcat ...
strcmp("Plain old stringTroy", "Plain old stringTroy") says: 0
如果兩個字符串參數相同,則strcmp返回0。 如果結果為0,則concat的行為與庫函數strcat完全相同。
這就是我對concat方法的看法。
#define MAXSIZE 32
void concat(char dest[], char src[])
{
int i=length(src);
int j=0;
for(j; j<src[j] !='\0'; j++) {
dest[i+j] = src[j];
}
dest[i+j] = '\0';
}
長度方法是:
int length(char str[])
{
// Add code here to return the length of the
// string str without using the strlen function
// Do not count the null character '\0'
// in computing the length of the string
int len=0;
int i;
for(i=0;i<str[i];i++) {
len++;
}
return len;
}
這是我的主要
int main()
{
// Variable declarations for all parts
char str2[] = "Troy";
char str4[] = "Plain old string";
char str6[MAXSIZE];
// Part 6
printf("\n----- Part 6 -----\n");
// Make a copy of the destination string first, to be reused later
strcpy(str6, str4);
concat(str4, str2);
strcat(str6, str2);
printf("Comparing results of concat and strcat ...\n");
printf("strcmp(\"%s\", \"%s\") says: %d\n",
str4, str6, strcmp(str4, str6)
);
return 0;
}
這是我運行時的輸出:
----- Part 6 -----
Comparing results of concat and strcat ...
strcmp("PlaiTroy", "Plain old stringTroy") says: -1
第一個字符串與第二個字符串不同,這就是我得到-1的原因。 我的問題在於我的concat方法,但我似乎無法理解為什么它不能很好地執行。 是因為空間嗎? 是0和'\\ 0'不能很好地執行?
您的代碼中存在多個問題:
length
函數中的循環測試不正確:而不是i < str[i]
,它應該是:
for (i = 0; str[i] != '\\0'; i++)
concat
函數中的相同問題。 將循環更改為:
for (j = 0; src[j] != '\\0'; j++) {
同樣在concat
函數中, i
應該是dst
的長度,而不是src
的長度。 對於此變量,您可以使用len
而不是i
。
函數main
中的數組str4
沒有任何空間可供concat
追加任何內容。 以這種方式用更大的尺寸定義它:
char str4[MAXSIZE] = "Plain old string";
這是更正后的版本:
#include <stdio.h>
#include <string.h>
#define MAXSIZE 32
void concat(char dest[], char src[]) {
int len = length(dest);
int j;
for (j = 0; src[j] != '\0'; j++) {
dest[len + j] = src[j];
}
dest[len + j] = '\0';
}
int length(char str[]) {
int len = 0;
int i;
for (i = 0; i < str[i]; i++) {
len++;
}
return len;
}
int main(void) {
// Variable declarations for all parts
char str2[MAXSIZE] = "Troy";
char str4[MAXSIZE] = "Plain old string";
char str6[MAXSIZE];
// Part 6
printf("\n----- Part 6 -----\n");
// Make a copy of the destination string first, to be reused later
strcpy(str6, str4);
concat(str4, str2);
strcat(str6, str2);
printf("Comparing results of concat and strcat ...\n");
printf("strcmp(\"%s\", \"%s\") says: %d\n",
str4, str6, strcmp(str4, str6));
return 0;
}
這兩個函數都有幾個問題:
concat
for(j; j<src[j] !='\0'; j++) {
這里的退出條件是什么?, src[j] != '\\0'
就足夠了。
dest[i+j] = src[j];
在這里添加偏移量為i
數據,但我是src
的長度,而不是dst
。
所以糾正的功能可能是:
void concat(char dest[], char src[])
{
/* descriptive variable name */
int len_dst = length(dst);
int j=0;
/* clear exit condition */
for(; src[j] != '\0'; j++) {
dest[len_dst+j] = src[j];
}
dest[len_dst+j] = '\0';
}
length
for(i=0;i<str[i];i++) {
同樣的說法,退出條件是什么? src[i] != '\\0'
就足夠了
所以糾正的功能可能是:
int length(char str[])
{
int len=0;
int i;
/* clear exit condition */
for ( i=0; str[i] != '\0'; i++) {
len++;
}
return len;
}
main
並在main
功能中發出警告:
char str4[] = "Plain old string";
concat(str4, str2); /* <- erases what is after str4 */
您沒有足夠的空間來存儲結果。 寫下類似的東西:
char str2[] = "Troy";
char str4[MAXSIZE] = "Plain old string"; /* <-- reserve spaces after str4*/
/* ... */
concat(str4, str2);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.