Arrays

Discussion in 'C++' started by ariffinaldo, Feb 28, 2010.

  1. ariffinaldo

    ariffinaldo New Member

    Joined:
    Feb 28, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hi all, This is my 1st post. I know i am asking for help inmy 1st post itself. Couldn't help it coz i just started C++ and well i came across a problem that i am unable to solve. I will appreciate if someone can help me. Not with an answer. Maybe at least the direction i should look towards. I need to do solve a problem where the function

    void changeStr(string& str1, string str2) and for
    test 1

    Test Case 1:
    string str1 = "ABCDEFGHI";
    string str2 = "123456789"
    then after the function call, str1 = “ABC123DEF456GHI789”

    Test Case 2:
    string str1 = "ABCDEFGHITU";
    string str2 = "123456789";
    then after the function call, str1 = “ABC123DEF456GHI789TU”

    Test Case 3:
    string str1 = "ABCDEFGHI";
    string str2 = "123456789IP";
    then after the function call, str1 = “ABC123DEF456GHI789IP”

    Test Case 4
    string str1 = "ABCDEFGHITU";
    string str2 = "123456789IP";
    then after the function call, str1 = “ABC123DEF456GHI789TUIP”

    My program which does not work for test 1 to 3 but works for test 4 only.

    Code:
    #include <iostream>
    #include<string>
    using namespace std;
    
    void changeStr(string& str1, string str2);	
    
    string str1 = "ABCDEFGHITU";
    string str2 = "123456789IP";
    
    int main(){
    	
            changeStr(str1,str2);
    	return 0;
    }
    void changeStr(string& str1, string str2)
    
    {
    	str1.insert(3,str2,0,3);
    	str1.insert(9,str2,3,3);
    	str1.insert(15,str2,6,3);
    	str1.insert(20,str2,9,2);
    	cout<<endl;
    	cout << str1;
    }
    
     
  2. ariffinaldo

    ariffinaldo New Member

    Joined:
    Feb 28, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    i managed to solve it though. But i feel it is not a good solution. Comments pls.

    Code:
    /*A program that reads in 2 strings and replaces the content of 1st string 
    by alternately appending 3 characters from each string to the new str 1.*/
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    void changeStr(string& str1, string str2);		//Function Prototype
    
    string str1 = "ABCDEFGHITU";		//Initialise Str 1
    string str2 = "123456789IP";		//Initialise Str 2
    int a = str1.length();				//initialise a to the length of str 1
    int b = str2.length();				//initialise b to the length of str 2
    
    
    int main(){
    	
    	changeStr(str1,str2);
    	return 0;
    }
    
    //changeStr(str1,str2);
     void changeStr(string& str1, string str2)
    {	
    	cout <<"String 1: "<<str1<<endl;	//display original string 1
    	cout <<"String 2: "<<str2<<endl;	//display original string 2
    
    	if ((a<=10)&& (b<=10)){
    	str1.insert(3,str2,0,3);	//insert 3 chars from pos[0]of str 2 to str 1 from pos[3]
    	str1.insert(9,str2,3,3);
    	str1.insert(15,str2,6,3);
    	}
    	else if((a<=10)&& (b>=10)){
    	str1.insert(3,str2,0,3);
    	str1.insert(9,str2,3,3);
    	str1.insert(15,str2,6,3);
    	str1.insert(18,str2,9,3);
    	}
    	
    	if((a>10)&& (b<=10)){
    	str1.insert(3,str2,0,3);
    	str1.insert(9,str2,3,3);
    	str1.insert(15,str2,6,3);
    	str1.insert(20,str2,9,2);
    	}
    	else if((a>10)&& (b>=10)){
    	str1.insert(3,str2,0,3);
    	str1.insert(9,str2,3,3);
    	str1.insert(15,str2,6,3);
    	str1.insert(20,str2,9,2);
    	}
    	cout <<"After function call, str1: ";
    	cout << str1;			//display the moified new string 1
    	cout <<endl;
    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice