Thread: somethingwrong with insert program

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    6

    Post somethingwrong with insert program

    Hi all,
    I wrote the following program to insert one string into another at a location specified by an integer n.
    The program compiles without errors but does not produce the desired results, just outputs : hello, when I want it to output heltherelo
    Please let me know waht I am doing wrong.
    Thanks a lot.


    #include<stdio.h>

    void main(void)
    {
    void insertstring (char str1[], char str2[], int n);
    char str1[] = {"hello"};
    char str2[] = {"there"};
    int n = 4;
    int i;

    insertstring("hello", "there", 3);
    printf("%s\n", str1);
    }

    void insertstring (char str1[], char str2[], int n)
    {
    int i, j, x, len1;
    char tmp[20];
    len1 = strlen(str1);


    /*copy str1 into tmp*/
    for(i = n, x = 0; i <= len1; i++, x++)
    tmp[x] = str1[i];

    /*copy str2 into str1*/
    for(j = 0; str2[j] != '\0'; j++)
    str1[n + j] = str2[j];

    str1[n + j + x] = '\0';
    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Try passing in the actual strings you created in your main function instead of explicitly writing them again...so change:

    insertstring("hello", "there", 3);

    to ..

    insertstring( str1, str2, 3 );
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    9
    Code:
    printf("%s\n", str1);
    will print hello unless you pass str1 as a parameter to "insertstring()".
    Code:
    insertstring(str1, "there", 3);
    Moreover, str1 should be created with enough space to accomodate the new string.

    Here's how it is done.

    [CODE]
    #include <stdio.h>
    #include <string.h>

    int main() {
    void insertstring(char s1[], char str2[], int n);
    char str1[20] = "hello", str2[] = "there";

    insertstring( str1, str2, 3 );
    printf("%s\n", str1);

    return 0;
    }

    // not really clean code
    void insertstring(char s1[], char s2[], int n) {
    char tmp[20];

    strncpy( tmp, s1, n );
    tmp[n] = '\0';

    strcat( tmp, s2 );
    strcat( tmp, &s1[n] );

    strcpy( s1, tmp );
    }
    [\CODE]

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I would use pointers to strings and ++ the pointers. Without them it is longer but works.

    Code:
    //call it with these vars
    char	*tmp=NULL;//pointer to a string so we always have the mem needed
    char	str1[]="test me",str2[]="add"; 
    int		n=2,len1,len2;
    
    	len1 = strlen(str1); 
    	len2 = strlen(str2); 
    	tmp=(char*)malloc(len1+len2+1);//alloc some mem, add one for the '\0'
    	if(tmp==NULL) return FALSE;//mem error
    
    	if(insertstring (str1, str2, tmp, n))//if returns TRUE there is no error
    	{
    		free(tmp);
    	}
    
    
    
    int insertstring (char *str1, char *str2, char *total, int n) 
    { 
    	int        i, len1,len2; 
    	len1 = strlen(str1); 
    	len2 = strlen(str2); 
    
    	if(n>=len1) return FALSE;//not enough chars in the first string to add it there
    
    	for(i = 0; i < (len1+len2); i++) 
    	{
    		if(i<n)  
    			total[i] = str1[i]; //copy in string 1 up to the point we need to add
    		else if( (i>=n) && (i<(n+len2)) ) 
    			total[i] = str2[i-n];  //copy in the second string starting at point n
    		else 
    			total[i] = str1[i-len2]; //add remainder of first string
    	}
    	total[len1+len2]='\0';//terminate
    	return TRUE;//good
    }
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    6
    Thank you guys. I just have a quick question on the correction. why is it does not print the changed string if I pass it to it the way I did.
    Thanks

  6. #6
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    For a simple reason that the contents of str1 are not stored where "hello" - insertstring("hello", "there", 3) is stored
    and
    the contents of str2 are not stored where "there" - insertstring("hello", "there", 3) is stored.

    Code:
    insertstring("hello", "there", 3);
    When called, the two strings "hello" and "there" are stored randomly somewhere in memory, and it is this address that you are trying to manipulate while the actual strings you want to modify are stored in: &str1[0] and &str2[0]
    Last edited by shaik786; 06-24-2002 at 06:14 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program using structs to calculate grades
    By TampaTrinDM88 in forum C Programming
    Replies: 4
    Last Post: 07-06-2009, 12:33 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. plz help me run this program!!!
    By galmca in forum C Programming
    Replies: 8
    Last Post: 02-01-2005, 01:00 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM