2809: 使用指针连接字符串。
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 94 Solved: 32
[ Submit][ Status][ Web Board]
Description
编写函数stringcat,实现字符串的连接,程序中需要使用指针形式访问字符串。提交函数部分即可。
Input
程序中给出的两个字符串。
Output
连接后的字符串。
Sample Output
I love C++
HINT
#include <iostream>
using namespace std;
char * stringcat(char *source, const char *dest)
{//将字符串dest的连接到字符串source的尾部
}
int main() {
char s1[30]="I love ";
char *s2="C++";
stringcat(s1,s2);
cout<<s1<<endl;
return 0;
}
这道题是在项目做不下去了的时候去做的...以后常刷指针题..练手..
#include <iostream>
#include <string>
using namespace std;
char *stringcat(char *source, const char *dest)
{
while(*source++) ; //空语句,使指针移到末尾
*source--; //向前移一位,因为上面结束前还向后移动一位
while(*dest!='\0')
{
*source++=*dest++; //把dest中的值赋给source
}
return 0;
}
int main()
{
char s1[30]="I love ";
char *s2="C++";
stringcat(s1,s2);
cout<<s1<<endl;
return 0;
}
运行结果: