Thread: Inserting a string

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    Inserting a string

    I have written a code which takes two strings s1 and s2 from the user and inserts the string s2 into s1 at a location m. But the code is not working properly, especially the first while loop.. can anyone figure out whats wrong...

    Code:
    #include<stdio.h>
    #include<string.h>
    
    void main()
    {
    	char s1[300],s2[100];
    	int m;int i,j;int k;
    
    	printf("Enter string one:");
    	gets(s1);
    
    	printf("Enter string 2:");
    	gets(s2);
    
    	printf("\nEnter location number:");
    	scanf("%d",&m);
    
    	i=strlen(s2);
    	j=strlen(s1)+1;
    	k=m;
    
    	while(s1[m])
    	{
    		s1[i+(j++)]=s1[m++];    /* create space for s2 in s1 by shifting the characters in s1 ahead by (i+j) positions from location m */
    	}
    
    	s1[i+j]=s1[m];  /* Insert the string terimator */
    	m=k;
    	i=0;
    	while(s2[i])
    	{
    		s1[m++]=s2[i++];     /* Copy string s2 into s1 from location m */
    	}
    
    	puts(s1);
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You might find that a whole lot easier to do with mmove().

    Look it up in your compiler's library documentation for more details.
    (Don't have that? ... Get it, you're going to need it a lot)

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    I need to know if there is anything wrong with the logic of this program... Why the first while loop is not creating space for string s2.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First think about what happens if you are moving 5 letters, with 10 left in the string...

    You can't start in the middle and work to the end... you'll overwrite half your string.
    You need to come at it from end and work back to the middle to avoid trashing it

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This should be a for loop. I could go on and on about it, and you could say you can do everything a for loop does with a while loop, but trust me - this should be a for loop:

    Code:
        while(s1[m])
        {
            s1[i+(j++)]=s1[m++];    /* create space for s2 in s1 by shifting the characters in s1 ahead by (i+j) positions from location m */
        }
    And the line in bold type should hit the dustbin, imo.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by CommonTater View Post
    First think about what happens if you are moving 5 letters, with 10 left in the string...

    You can't start in the middle and work to the end... you'll overwrite half your string.
    You need to come at it from end and work back to the middle to avoid trashing it
    I am overwriting the data after I have moved it ahead... How am I trashing it??

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    This is the version of my program which works... But I still need to know if there was anything wrong with my first program..


    #include<stdio.h>
    #include<string.h>
    Code:
    void main()
    {
    	char s1[300],s2[100];
    	int m;int i,j;int k;
    
    	printf("Enter string one:");
    	gets(s1);
    
    	printf("Enter string 2:");
    	gets(s2);
    
    	printf("\nEnter location:");
    	scanf("%d",&m);
    
    	k=m;
    	i=strlen(s1);
    	j=strlen(s1)+strlen(s2);
    	
    	while(i>=m)
    		s1[j--]=s1[i--];
    
    	m=k;
    	i=0;
    	while(s2[i])
    	{
    		s1[m++]=s2[i++];
    	}
    
    	puts(s1);
    }

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Two easy ways to find out what was wrong with your earlier code:

    1) Use your debugger, step through the code, and watch the values change within the strings, as you do so.

    2) Add a print statement of i, j, and m, and the string, inside the first while loop, along with a getchar() to pause the display, after every iteration of the loop.

    Having less complex code is certainly an advantage if you need to debug, extend, or ask someone else to study or critique your code.

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Don't waste your fingers, hk_mp5kpdw. juice is like the honey badger; he just don't give a .........

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by juice View Post
    I am overwriting the data after I have moved it ahead... How am I trashing it??
    Ok you have "The quick brown jumped over the lazy dog in the field"
    Now you want to insert "fox" after brown...

    So the first human reflex is to push everything foward 4 spaces and insert "fox" and a space...
    So you create a loop to start at the j in jumped and move everything forward by 4 spaces...
    Now you've got about 30 characters to move.... here's what happens...

    PHP Code:
    the quick brown jumped over the lazy dog in the field

    1
    the quick brown jumpjed over the lazy dog in the field
    2
    the quick brown jumpjud over the lazy dog in the field
    3
    the quick brown jumpjum over the lazy dog in the field
    4
    the quick brown jumpjumpover the lazy dog in the field
    5
    the quick brown jumpjumpjver the lazy dog in the field
    6
    the quick brown jumpjumpjuer the lazy dog in the field
    ...
    until the outcome is:

    The quick brown jumpjumpjumpjumpjump ... 
    By the time you move the 5th character you're already copying characters that have already been moved once.

    That's why I suggested coming at it from the end...
    PHP Code:
    the quick brown jumped over the lazy dog in the field

    1
    the quick brown jumped over the lazy dog in the fiel    d
    2
    the quick brown jumped over the lazy dog in the fie    ld
    3
    the quick brown jumped over the lazy dog in the fi    eld
    4
    the quick brown jumped over the lazy dog in the f    ield
    ...

    Until you have

    the quick brown    jumped over the lazy dog in the field 
    Now you can insert fox and have it work...
    Last edited by CommonTater; 12-12-2011 at 11:03 AM.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by rags_to_riches View Post
    Don't waste your fingers, hk_mp5kpdw. juice is like the honey badger; he just don't give a .........
    After 142 posts and still using gets and void main, you're probably right. He does seem to have learnt proper indentation though.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by CommonTater View Post
    Ok you have "The quick brown jumped over the lazy dog in the field"
    Now you want to insert "fox" after brown...

    So the first human reflex is to push everything foward 4 spaces and insert "fox" and a space...
    So you create a loop to start at the j in jumped and move everything forward by 4 spaces...
    Now you've got about 30 characters to move.... here's what happens...

    PHP Code:
    the quick brown jumped over the lazy dog in the field

    1
    the quick brown jumpjed over the lazy dog in the field
    2
    the quick brown jumpjud over the lazy dog in the field
    3
    the quick brown jumpjum over the lazy dog in the field
    4
    the quick brown jumpjumpover the lazy dog in the field
    5
    the quick brown jumpjumpjver the lazy dog in the field
    6
    the quick brown jumpjumpjuer the lazy dog in the field
    ...
    until the outcome is:

    The quick brown jumpjumpjumpjumpjump ... 
    By the time you move the 5th character you're already copying characters that have already been moved once.

    That's why I suggested coming at it from the end...
    PHP Code:
    the quick brown jumped over the lazy dog in the field

    1
    the quick brown jumped over the lazy dog in the fiel    d
    2
    the quick brown jumped over the lazy dog in the fie    ld
    3
    the quick brown jumped over the lazy dog in the fi    eld
    4
    the quick brown jumped over the lazy dog in the f    ield
    ...

    Until you have

    the quick brown    jumped over the lazy dog in the field 
    Now you can insert fox and have it work...
    Thanx common. It was silly of me..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting contents of string into an array
    By Gaming in forum C++ Programming
    Replies: 4
    Last Post: 04-13-2008, 10:31 AM
  2. inserting charcters in a string
    By Calavera in forum C Programming
    Replies: 17
    Last Post: 10-10-2004, 09:40 AM
  3. Inserting a character in the middle of a string
    By winsonlee in forum C Programming
    Replies: 1
    Last Post: 03-20-2004, 04:35 AM
  4. Replies: 0
    Last Post: 04-05-2003, 09:33 AM
  5. Inserting string
    By mellisa in forum C++ Programming
    Replies: 5
    Last Post: 01-19-2003, 02:20 AM