+ Reply to Thread
Results 1 to 2 of 2

Thread: String insertion in C as part of a string and pointer tutorial

  1. #1
    Newbie Yuriy M is an unknown quantity at this point Yuriy M's Avatar
    Join Date
    Sep 2007
    Location
    Winnipeg, Canada
    Age
    27
    Posts
    8

    Question String insertion in C as part of a string and pointer tutorial

    Hi guys. It's been a while since I last posted here.

    I'll cut straight to the chase. I've been brushing up on my C programming for the past year and have been working through an old textbook. One of the questions in that textbook asks the following:

    Code:
    Write a program that inserts one string into another. The program should prompt the user for a string. Then the program should prompt the user to enter another string that is to be inserted into the first string. Finally, the program should prompt the user for the position at which to insert the second string. The program should then do the insertion and display the new string. For example, suppose that the user enters for the first string "I left heart in Francisco", for the second string, "San ", and for the position, 16. Then the program should output "I left heart in San Francisco".
    Here is the program that I've written up:

    Code:
    /* Program 12-9 */
    
    #include <stdio.h>
    
    main()
    {
          /* Declare variables */
          
          char string[100],            /* The string to be inputted by the user */
               insert[100],            /* The string to be inserted into the other string */
               inserted[200] = {' '},  /* The resulting string after insertion */
               * str_ptr = string,     /* The pointer to the string array */
               * ins_ptr = insert,     /* The pointer to the insert array */
               * instd_ptr = inserted; /* The pointer to the inserted array */
          int  position,               /* The specific position for the second string to be inserted into the first */
               string_count = 0;       /* Counts the characters in the string */
          
          /* Prompt the user for the first string */
          
          printf("\nEnter a string: ");
          gets(string);
          
          /* Prompt the user for the second string to be inserted into the first */
          
          printf("\nEnter a string to be inserted into the first string: ");
          gets(insert);
          
          /* Prompt the user for the position of the second string to be inserted */
          
          printf("\nEnter the numerical position of the string to be inserted into the first string: ");
          scanf("%d", &position);
          
          /* Check both strings and insert the second string into the specific position */
          
          while (*instd_ptr != '\0')
          {
           while (*str_ptr != '\0')
           {
            if (string_count == position)
            {
             while (*ins_ptr != '\0')
             {
              *instd_ptr = *ins_ptr;
              ++ins_ptr; 
              ++instd_ptr;  
              ++string_count;
             }
            }
            *instd_ptr = *str_ptr;
            ++str_ptr;
            ++instd_ptr;  
            ++string_count;
           }
          }
           
          /* Display the results */
          
          printf("\nHere is the result of your insertion: ");
          printf("%s", inserted);
          printf("\n");
    }
    I've tested the program out by inserting the second string at the very beginning, middle, and end of the first string. The program question expects you to also leave a space after you type in the second string so it would make for easier string placement. However, it shows me an example of putting one string in the middle of another. What I want to know is if the last character(s) inputted as a space in the first string is applicable for putting in the second string?

    Let's say for instance that I just type in "I left heart in San" as the first string and "Francisco" as the second string. As the rules state, I have to put "Francisco" in a specific position within "I left heart in San". The position I want is two spaces after "San". If I just type in "I left heart in San" and chose position 20, the program would just output it as "I left heart in San" and ignore "Francisco" as a result of the NULL character after "n". However, if I type in "I left heart in San ", the program will definitely put in "Francisco" because there is an existing character placement in position 20 which is the blank space. As long as there isn't a NULL character prior to that space, all I need is one character "F" to fill that space and the program will print out "Francisco" without any problems.

    I also want to point out that such functions like strncat(), strcat() and strlen() are not supposed to be used just yet. They would be covered in the next chapter of my textbook. Basically, the author just wants to see if I can even get a program working using the limitations that I have now.

    So, I just want to ask is if the code that I typed sufficient enough to meet that question's requirements?
    For $1000: Something that is a miserable pile of secrets.

  2. #2
    Newbie Yuriy M is an unknown quantity at this point Yuriy M's Avatar
    Join Date
    Sep 2007
    Location
    Winnipeg, Canada
    Age
    27
    Posts
    8

    Exclamation Re: String insertion in C as part of a string and pointer tutorial

    Well, if nobody is going to reply, then I'll just have to assume that the program is completed and move on.
    For $1000: Something that is a miserable pile of secrets.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts