簡體   English   中英

在不使用內置 strcpy 的情況下實現 strcpy 函數

[英]Implementing strcpy function without using in-built strcpy

編輯:問題已解決,我非常感謝所有幫助過我的人,如果您將來看到這篇文章,這里是在 C 中實現 strcpy函數的工作代碼:

#include <stdio.h>
void strcopy(char *a, char *b)
{
    for (int i = 0; a[i] != '\0'; i++)
    {
        b[i] = a[i];
        if(a[i+1] == '\0')
            b[i+1]='\0';
    }
}
int main()
{
    printf("Enter the string: ");
    char a[10];
    scanf("%s",&a);
    printf("Input string: %s", a);
    char b[sizeof(a)]; 
    strcopy(a,b);
    printf("\nOutput string: %s", b);

    return 0;
}

輸出:

Enter the string: helloworld
Input string: helloworld 
Output string: helloworld



問題從這里開始:

我正在嘗試在不使用內置函數的情況下實現 strcpy 函數。 我在網上搜索過,但我只找到了執行此任務的程序,但沒有找到功能

我嘗試了各種方法,但沒有運氣。 最后(嘗試 4),我寫了一個有效的代碼,但我不明白為什么這東西有效,但以前的方法卻沒有

只是尋找一個函數,它可以在從 main() 調用時將一個字符串的值復制到另一個字符串中,並在輸出字符串中顯示正確的值。 如果我能得到它,我將非常感激。 當然,我不能使用內置函數,因為我被迫在星期一進行考試,因為我們必須實現隨機 7 個字符串函數中的任何一個並解釋代碼。

編輯:還發現這個https://www.programmersought.com/article/29163684184/代碼來實現同樣的事情,但它是用 C++ 編寫的。 為什么在 C++ 發明之后學校/學院仍然教授 C? 任何人都知道任何 C++ 到 C 轉換器?

嘗試 1:

#include <stdio.h>
void strcopy(char *a, char *b)
{
    for (int i = 0; a[i] != '\0'; i++)
    {
        b[i] = a[i];
    }
}
int main()
{
    char a[10];
    printf("Enter string: ");
    scanf("%s", &a);
    printf("Input string: %s", a);
    char b[(sizeof(a))];
    strcopy(a,b);
    printf("\nOutput string: %s", b);

    return 0;
}

輸出:

Enter string: helloworld
Input string: helloworld
Output string: helloworldhelloworld





嘗試2:

#include <stdio.h>
void strcopy(char *a, char *b)
{
    for (int i = 0; a[i] != '\0'; i++)
    {
        b[i] = a[i];
    }
}
int main()
{
    int n;
    printf("Enter the string size: ");
    scanf("%d",&n);
    char a[n];
    printf("Enter string: ");
    scanf(" %s",&a);
    printf("Input string: %s", a);
    char b[n]; 
    strcopy(a,b);
    printf("\nOutput string: %s", b);

    return 0;
}

輸出:

Enter the string size: 7
Enter string: helloworld
Input string: helloworld  
Output string: helloworldö





嘗試3:

#include <stdio.h>
void strcopy(char *a, char *b)
{
    for (int i = 0; a[i] != '\0'; i++)
    {
        b[i] = a[i];
    }
}
int main()
{
    int n;
    printf("Enter the string size: ");
    scanf("%d",&n);
    n++;
    char a[n];
    printf("Enter string: ");
    // scanf(" %s",&a);
    fgets(a,n,stdin);
    fgets(a,n,stdin);  //need to write twice or it wont work
    printf("Input string: %s", a);
    char b[n]; 
    strcopy(a,b);
    printf("\nOutput string: %s", b);

    return 0;
}

輸出:

Enter the string size: 5
Enter string: helloworld
Input string: hello  
Output string: hello⌂





嘗試4:

#include <stdio.h>
void strcopy(char *a, char *b)
{
    for (int i = 0; a[i] != '\0'; i++)
    {
        b[i] = a[i];
    }
}
int main()
{
    int n;
    printf("Enter the string: ");
    scanf("%d",&n);
    n++;
    char a[n];
    scanf(" %s",&a);
    // printf("Enter string: ");
    // fgets(a,n,stdin);
    // fgets(a,n,stdin);
    printf("Input string: %s", a);
    char b[n]; 
    strcopy(a,b);
    printf("\nOutput string: %s", b);

    return 0;
}

輸出:

Enter the string: helloworld
Input string: helloworld 
Output string: helloworld





編輯:

朋友在回答中給出的嘗試方法:

代碼:

#include <stdio.h>
void strcopy(char *a, char *b)
{
    for (int i = 0; a[i+1] != '\0'; i++)
    {
        b[i] = a[i];
    }
}
int main()
{
    // int n;
    printf("Enter the string: ");
    // scanf("%d",&n);
    // n++;
    char a[10];
    scanf("%s",&a);
    // printf("Enter string: ");
    // fgets(a,n,stdin);
    // fgets(a,n,stdin);
    printf("Input string: %s", a);
    char b[sizeof(a)]; 
    strcopy(a,b);
    printf("\nOutput string: %s", b);

    return 0;
}

輸出:

Enter the string: helloworld
Input string: helloworld
Output string: helloworl





編輯2:

嘗試 do-while 循環:

代碼:

#include <stdio.h>
void strcopy(char *a, char *b)
{
    // for (int i = 0; a[i+1] != '\0'; i++)
    // {
    //     b[i] = a[i];
    // }
    int i=0;
    do{
        b[i]=a[i];
        i++;
    }while(a[i] != '\0');
}
int main()
{
    // int n;
    printf("Enter the string: ");
    // scanf("%d",&n);
    // n++;
    char a[10];
    scanf("%s",&a);
    // printf("Enter string: ");
    // fgets(a,n,stdin);
    // fgets(a,n,stdin);
    printf("Input string: %s", a);
    char b[sizeof(a)]; 
    strcopy(a,b);
    printf("\nOutput string: %s", b);

    return 0;
}

輸出:

輸入字符串:helloworld 輸入字符串:helloworld 輸出字符串:helloworldhelloworld

OP 的strcopy()在嘗試模仿標准庫函數時存在各種問題:

char *strcpy(char * restrict s1, const char * restrict s2);

目標數組中缺少空字符

這是 OP 的主要問題。 必須以某種方式在目標中分配一個空字符 OP 的各種代碼都沒有做到這一點。

提示:考慮復制一個字符串""它只為目標分配一個空字符'\\0'

錯誤的參數順序

第一個參數應該是匹配strcpy()的目標

缺少返回值

strcpy()返回原始目標指針。

高級:參數缺少restrict/const屬性

只需使用上面的庫函數簽名。

高級:字符串失敗

OP 的代碼使用int來索引這對於字符串是不夠的。 最好使用size_t

提示:添加輸出清晰度

打印結果時,添加標記字符,如< >和最后的'\\n'
對於調試工作,將目標字符串設為 2 倍大。

// char b[sizeof(a)]; 
char b[sizeof(a) *2]; 
strcopy(a,b);
// printf("\nOutput string: %s", b);
printf("\nOutput string: <%s>\n", b);

OP 在不是家庭作業編碼任務中斷言這一點,因此候選解決方案:

char *strcpy(char * restrict s1, const char * restrict s2) {
  // Use unsigned char as all str functions behave as if char was unsigned
  // Also need to save s1 for return
  unsigned char *us1 = (unsigned char *)s1;
  const unsigned char *us2 = (const unsigned char *)s2;

  while (*us2) {
    *us1++ = *us2++;
  }

  return s1;
}

在 K&R 為王的過去,我們會在腰帶上系一個洋蔥,就像當時的風格一樣,並做一些類似的事情

void strcopy(char *a, char *b)
{
   while ( *b++ = *a++ ) {}
}

現在我認為舊的方式已經貶值了

有人建議我解釋為什么會這樣。

*b = *a做復制一個字節的核心功能

*b++ = *a++添加自動增量,在賦值完成后將兩個指針移動到內存中的下一個位置

while (*b++ = *a++) {}循環,而復制的字節不為零。 空的{}只是為了完成while語句的語法

因此,while 循環將運行復制字節,然后將兩個指針遞增到下一個位置,直到復制的字節為零,即字符串的結尾

您沒有添加終止\\0

你所有的實現都有相同的基本錯誤; “try 4”僅在偶然情況下起作用。 問題在於strcopy的循環終止條件。 當您到達結束源字符串的 NUL 字符時,您將停止循環而不是復制它。 這意味着目標字符串沒有結束並且printf越過它的末尾,打印任何發生在內存中的內容。 您必須改為復制 NUL ,然后停止循環。

您在如何為字符串ab分配內存方面也有錯誤——在每個實現中都不同,但在我看來它們都沒有抽象正確。 與您的講師討論如何正確執行此操作,這是 C 中最復雜的主題之一,我沒有足夠的信息說明您可以使用哪些技術。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM