[英]C++ How to append char to previously reversed char?
你好,我需要插入/追加/連接功能。 如何將反向字符添加到我的“主字符”? 示例輸出已注釋。
這是我的功能代碼和主要內容:
void app(char *str2,const char *str1){
const char *temp=str1;
char* temp_snd=str2;
const char*temp_trd=str1;
char *temp_fth;
while(*temp_trd){
temp_trd++;
}
while(temp_trd!=str1){
temp_trd--;
*temp_fth=*temp_trd;
temp_fth++;
}
*temp_fth='\0';
while(*temp&&*temp_snd){
temp++;
temp_snd++;
}
while(temp!=str1&&temp_snd!=temp){
temp--;
temp_snd++;
*temp_snd=*temp;
}
strcat(temp_snd, temp_fth);
}
這是我的主要內容:
int main(int argc, const char * argv[]) {
const char *str1 = "seY ";
char str2[20] = "Hello";
cout << str2 << endl; // Hello
app(str2, str1);
cout << str2 << endl; // Hello Yes
app(str2, "llor ");
cout << str2 << endl; // Hello Yes Roll
return 0;
}
這是我的輸出:
Hello
Hello Yes
Hello roll
我知道我的職責不是完美的,而是富有同情心。 請。
你可以試試這個:
void add (char x[])
{
int n;
cin >> n; // Length of string
char*y=new char[n];
cin >> y;
reverse(y, y+strlen(y));
strcat(x, " ");
strcat(x, y);
}
int main()
{
int n;
cin >> n; // Length of base string
char *x=new char[n];
cin >> x;
add(x);
cout << x << endl;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.