Thread: Inserting a character in the middle of a string

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    41

    Inserting a character in the middle of a string

    I would like to insert a * in the middle of the sentence so that the output will be like '---*|abc'.
    I had code it out but the output doesnt seems like the one i wanted.

    It produce this output ¨d¨d¨d---*|abc instead of just ---*|abc . I would like to know which part of the code went wrong or is there any other easier way that i can code to archieve the same objective ??

    Code:
    main(int argc,  char *argv[])
    {
    
    	int c;
    	int i=0;
    	char sentence[] = "---|abc";
    
                    textFormatting(sentence);
    
    
    
    }
    
    void textFormatting(char *garbage)
    {
    
    int j,i;
    int count;
    char temp;
    
    count = strlen(garbage);
    
    
    	for(j=0 ; j < count; j++)
    	{
    		if(*garbage != '-') // j = 3
    		{
    		garbage--;
    
    		if(*garbage == '-')
    		{
    	                        garbage++;
    			for(i = j ; i < count; i++)
    			{
    			garbage++;
    			}
    			
    
    			for(i = j ; i < count + 1 ; i++)
    			{
    			temp = *garbage;
    			garbage++;
    		                *garbage = temp;
    			garbage--;
    			garbage--;				if( i == count)
    		{
    		garbage++;
    		*garbage = '*';
    		}
    	}
    						
    	}
    	}
    	garbage++;
    
    
    	}
    
    for(i=0 ; i < count ; i++)
    {
    garbage--;
    
    }
    printf("%s\n", garbage);
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > char sentence[] = "---|abc";
    Well you can't insert anything into this string, since it is already full.

    It's generally easiest to use two strings
    char sentence[] = "---|abc";
    char new[100];
    textFormatting(sentence,new,sizeof new);

    Within textFormatting(), you can use things like strncpy(), strcpy(), strcat() to move bits of strings from one to the other.

    I think this does it
    Code:
    char *p = strchr( src, '|' );
    int len = p - src;
    strncpy( dst, p, len );
    dst[len] = '\0';
    strcat( dst, "*" );
    strcat( dst, p );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM