Thread: Inserting string

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    37

    Inserting string

    Let's say I have a string (the current line in an array)

    And the user want to insert a new string into a certain position of that string ... say... after the 5th character.

    Can I use strncopy (CopyTo, CopyFrom, Numcells)
    OR strncpy (Append To, AppendFrom, Numcells) ??

    What is "Numcells" suppose to be, the position of where to insert the new string?

    This is what I tried

    Code:
    ......
    
     cout <<"Where on the current line"; 
     cout<<"do you want to insert a new string?"<<endl;
     cin>> position;                          
     cout<<"enter the string to be inserted: "<<endl;
     cin.getline(temporary_array[0], 80, '\n');
     strncpy (array[i-1],temporary_array[0],position);
     
    		
    cout<<"The current line is now: "<<array[i-1]<<endl;
    
    .....
    First of all, the whole program just exits as soon as it reaches "enter the string to be inserted"
    But if I have this:

    Code:
     cout<<"enter the string to be inserted: "<<endl;
     cin.getline(temporary_array[0], 80, '\n');
     strncmp (array[i-1],temporary_array[0],6);
    
     cout<<"The current line is now: "<<array[i-1]<<endl;
    It just overwrites the first 5 characters of the original line.
    I shouldnt be using strncpy() or what?
    Last edited by mellisa; 01-19-2003 at 12:06 AM.

  2. #2
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	string str1("The first string");		
    	string str2("and second ");
    
    	str1.insert(10, str2);
    
    	cout << str1 << endl;
    
    	return 0;
    }
    Well - judging from your post your not actually using string objects - so this may be adding something into the mix that you are not interested in trying. But the string object contains a rather easy to use insert() method. You could perhaps use an array of strings?

    strncmp() is used for comparing character arrays - not for appending them. That would be strcat().

    You can do something like this using the cstring functions:
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main( void )
    {
       char input[100] = "Cats are nice usually";
       cout << "Before: " << input << endl;
       strncpy( input, "Dogs", 4 );
       strncpy( input + 9, "mean", 4 );
       cout << "After: " << input << endl;
    
       return 0;
    }
    Last edited by foniks munkee; 01-18-2003 at 11:57 PM.
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    37
    thanks, just realised "strncmp" was a typo

    I sortta understand what you meant , however the string to be inserted will be input by the user. So I would think we need some sort of a temporary array to store that first.

  4. #4
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    This *might* help or something similar
    Code:
    #include <iostream.h>
    
    int main(void)
    {
    	char user1[80];
    	char user2[80];
    	char temp[256];
    	
    	temp[0] = '\0';
    	
    	int loc, i, j;
    	
    	cout << "Enter string1: ";
    	cin.getline(user1, 80); 
    	
    	cout << "Enter string2: ";
    	cin.getline(user2, 80);
    	
    	cout << "Enter loc where to place string 2 into string 1: ";
    	cin >> loc;
    	
    	j=0; 
    	
    	for(i=0; i < loc-1; i++)
    		temp[j++] = user1[i];
    	
    	for(i=0; user2[i] != '\0'; i++)
    		temp[j++] = user2[i];
    	
    	for(i=loc-1; user1[i] != '\0'; i++)
    		temp[j++] = user1[i];
    	
    	temp[j] = '\0';
    	
    	cout << endl << temp << endl; 
    			
    	return 0;
    }
    You'd need to code in some protection for the strings that you join, so that temp wouldn't be overflowed, user1 had something worth inserting, or loc was beyond user1['\0'].

    The string library would be better as would a linked list of some sort I'd imagine.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    37
    Ronin - yes that is almost exactly what I'm looking for and I just
    realised what I tried to do before with the "strncpy" is just to
    replace the whole string into the original string

    anyway, your program works , but the last 3 for loops blew me
    right out of the water, can you explain abit?

    Code:
    	j=0; 
    	
    	for(i=0; i < loc-1; i++)
    	temp[j++] = user1[i];
    
    // copy user1 array into temp ?
    	
    	for(i=0; user2[i] != '\0'; i++)
    	temp[j++] = user2[i];
    
    // copy user2 array into temp?? Then i look at the next loop and   
    think, that can't be it.
    	
    	for(i=loc-1; user1[i] != '\0'; i++)
    	temp[j++] = user1[i];

  6. #6
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    NP...

    j=0;

    for(i=0; i < loc-1; i++)
    temp[j++] = user1[i];

    // copy user1 array into temp ?


    yep.. copy elements 0-loc. But remember the array is indexed one less than your user requested for the entry point, so subtract 1 from your user's request.

    for(i=0; user2[i] != '\0'; i++)
    temp[j++] = user2[i];

    // copy user2 array into temp?? Then i look at the next loop and
    think, that can't be it.


    yep that's exactly what it did. You have left user1 at the desired location, so now you can copy the contents of user2 into temp, but maintain temp[j++] so that you keep its placement for the next operation

    for(i=loc-1; user1[i] != '\0'; i++)
    temp[j++] = user1[i];


    No biggie here... You know where you left of in user1 with the variable loc. All you need to do is to start the copy process at that point you left off in until you encounter the null terminator of '\0' in user1. Just remember to terminate temp when the process is done. That's what temp[j] = '\0'; does

    HTH
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM