Thread: problem with string copy and concatanate

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    12

    problem with string copy and concatanate

    i have this program that i need to have the user enter in how ever many words and i have to take the frist word and concatanate it with the last word...

    do if they first enter in goodbye and the last word they enter is day the output should say goodday..

    i have to use the string copy and then concatanate the two and i just cant seem to get it right.....?

    could you please help me out...
    Im not the smartest but i try

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    First of all you need to indicate what type of string you are using. If it is a null terminated char array then you need to functions in the string.h (or cstring header) file (depending on your compiler) to manipulate the strings. If you are using strings from the STL string class or some other string class then there are probably methods (functions built into the class) to do the manipulations.

    If you concatenate day onto goodbye you goodbyeday, not goodday.



    char word1[40] = "hello";
    char word2[] = " world";
    char word3[50];

    strcat(word1, word2);
    strcpy(word3, word1);

    cout << word3;

    should put the phrase hello world on the screen. so should the following using the string class from STL.

    string word4 = "hello";
    string word5 = " world";
    string word6;

    word4 = word4 + word5;//concatenation
    word6 = word4;//copying one string to another

    cout << word6;

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    12
    well i know if you concatanate goodbye and day you get goodbyeday but i need it to say goodday if that was the words that someone would enter...
    Im not the smartest but i try

  4. #4
    Unregistered
    Guest
    this is what i got can you guys figure it out from here where im going wrong... thanks this is making me pull out all of my hair.....



    #include<iostream.h>
    #include<string.h>
    char * replace(char *source, char *substring, char *replace);
    void main()
    {
    char string[100]={0};
    char mystring[95]={0};
    char yourstring[90]={0};
    char newstring[100]={0};
    char* result;
    cout<<"Enter in a character string: "<<endl;
    cin>>string;
    cout<<"Enter in another character string: "<<endl;
    cin>>mystring;
    cout<<"Enter in one last character string: "<<endl;
    cin>>yourstring;
    result=replace(string,mystring,yourstring);
    cout<<result<<endl;
    }


    char * replace(char *source,char *substring,char *replace);
    {
    strncpy(char* string,size_4)=newstring;
    cout<<strncat(newstring,yourstring);

    }

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    12
    up there......

    that last post was me even though it said unregistered or what ever i was in class and i still cant seem to comprehend this program to get it to work take a look at my code and alter it a little and see if you guys can get it going...
    Im not the smartest but i try

  6. #6
    Unregistered
    Guest
    you guys should help him out he seems like a good guy............................................... .

    or gal......................

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    12
    can your guys come up with any results?
    Im not the smartest but i try

Popular pages Recent additions subscribe to a feed