Thread: string insertion into another string....

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    10

    string insertion into another string....

    Sorry... another post... I'm so sick of tired of doing my C programming assignment. When will I achieve the level of your programming skill....

    Here is the programe, and fatal error on the while loop is there anything wrong with the program, or is there any simpler way to solve this....

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    void insertsub (char str[]);
    
    main()
    {
      char str[100];
      printf ("Enter the String :");
      fgets (str, sizeof str, stdin);
      printf ("\n");
      insertsub (str);
      return 0;
    }
    
    void insertsub (char str[])
    {
        char substr[100], temp[100];
        char *start, *end, *p, sp;
        int i;
    
        printf ("Enter the substring you want to insert: ");
        fgets (substr, sizeof substr, stdin);
        p = strchr(substr, '\n');
        if (p) *p = '\0';
        printf ("Enter the starting character in the string for the insertion: ");
        scanf("%c", &sp);
    
        end = strchr (str, '\0');
        start = strchr(str, sp);
            if (start)
            {
                printf ("String not Found !!!");
            }
    
    
        for (start, i=0; *start!='\0'; start++, i++)
            {
                start = &temp[i];
    
                while (end != strchr(str, sp))
                end--;
            }
            *(end + 1) = '\0';
    
            strcat (str, substr);
            strcat (str, temp);
    
            printf ("The output of the string with sub string inscerted: %s ", str);
    }
    It's the flaws that makes the perfect more perfect....

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    try something like that:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void) {
    	char str[100], add[100],*p, start;
    	int i = 0;
    
    	printf("Enter the original string: ");
    	fgets(str,sizeof(str),stdin);
    	p = strchr(str,'\n');
    	if (p) *p = '\0';
    
    	printf("Enter the string to be cat: ");
    	fgets(add,sizeof(add),stdin);
    	p = strchr(add,'\n');
    	if (p) *p = '\0';
    
    	printf("The char to start adding: ");
    	scanf("%c",&start);
    
    	p = strchr(str,start);
    	if(!p) {
    		printf("Not found\n");
    		return 0;
    	}
    
    	else {
    		
    		for (i=0; i<strlen(add); i++)
    			*p++ = *(add+i);
    	}
    
    	printf("After adding: %s\n",str);
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    10

    how about the character after the insertion string, it doesn't push backwords...

    Thanks for your reply, the programe you did is much better and neater compare to mine except that the add string will copy over the original string.

    What I did here is to create a temp array to store the character after the starting point of insertion and delete the character after the starting point in the original array.

    With the use of start pointer, I copy the character to temp array, and with the decreasment of end pointer, I insert the '\0' to delete rest of the string.

    After that with the use of strcat to append the add array back to the original array then follow by the temp array.

    But... why it still go wrong...

    Sigh...

    Oh hay... Vber, I love your qoute but what's Lisp means...
    It's the flaws that makes the perfect more perfect....

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I don't know if the following will clarify or obfuscate.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char *insertsub (char *original, const char *search, const char *replace);
    
    int main(void)
    {
        /* I'll forgo user input for simplicity here. */
        const char text[] = "Here is the text I want to begin with.";
        char str[100];
        strcpy(str, text);
        printf("str = \"%s\"\n", str);
        printf("str = \"%s\"\n", insertsub(str, "text", "new string"));
        printf("str = \"%s\"\n", insertsub(str, "begin", "end"));
        return 0;
    }
    
    char *insertsub (char *original, const char *search, const char *replace)
    {
        char *found = strstr(original, search);
        if ( found != NULL )
        {
            size_t olen = strlen(original);
            size_t slen = strlen(search);
            size_t rlen = strlen(replace);
            memmove(found + rlen, found + slen, olen - (found - original) + 1);
            memmove(found, replace, rlen);
        }
        return original;
    }
    
    /* my output
    str = "Here is the text I want to begin with."
    str = "Here is the new string I want to begin with."
    str = "Here is the new string I want to end with."
    */
    [edit]Removed unused header.[/edit]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Well, about what LISP means:
    A programming language designed to process data consisting of lists. It is widely used in artificial intelligence research. lis(t) p(rocessing)
    To understand the quote, you need to know lisp.
    About the real question, Dave gave you an example.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 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