1 solutions

  • 0
    @ 2025-11-5 16:11:05

    C :

    #include <stdio.h>
    void copy(char *pa,char *pb);
    int main(void)
    {
    	char a[80],b[80];
    	gets(a);
    	printf("OldString=%s",a);
    	copy(a,b);
    	printf("\nNewString=%s",b);
    	return 0;
    }
    void copy(char *pa,char *pb)
    {
    	while(*pa != '\0')
    	{
    		*pb=*pa;
    		pa++;
    		pb++;
    	}
    	*pb='\0';
    }
    

    C++ :

    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
    	char s1[80];
    	cin>>s1;
    	char *s2 = new char[strlen(s1)+1];
    	strncpy(s2,s1,strlen(s1));
    	cout<<"OldString=" << s1 << endl;
    	cout<<"NewString="<< s2 << endl;
    	delete []s2;
    	return 0;
    }
    
    
    • 1

    【设计型】第9章: 指针 9.14 字符串的拷贝

    Information

    ID
    17549
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By