简体   繁体   中英

C++ How to append char to previously reversed char?

Hello I need to make insert/append/concat function. How to add reversed chars to my "main char"? Example output is commented.

Here is my func code and main:

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);
}

and this is my main:

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;
}

Here is my output:

Hello
Hello Yes
Hello  roll

I know that my function is no perfect made but be compassionate. Please.

You could try this:

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM