Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 12

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

  1. #1
    Yuriy M's Avatar
    Yuriy M is offline Learning Programmer
    Join Date
    Sep 2007
    Location
    Winnipeg, Canada
    Posts
    70
    Rep Power
    0

    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. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Yuriy M's Avatar
    Yuriy M is offline Learning Programmer
    Join Date
    Sep 2007
    Location
    Winnipeg, Canada
    Posts
    70
    Rep Power
    0

    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.

  4. #3
    karthik is offline Newbie
    Join Date
    Feb 2011
    Posts
    1
    Rep Power
    0

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

    Fantastic JOb!!
    this helped me to complete my project
    thank you very much

  5. #4
    jcampos8782 is offline Learning Programmer
    Join Date
    Jun 2009
    Posts
    49
    Rep Power
    0

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

    The only problem that I could see is that you are representing the strings as finite length arrays instead of pointers to char. If you represent them as pointers to char, then you will not be forced to stick within a set size requirement.

    Code:
          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 *
    Instead, try something like this:
    Code:
          
               char* string1 = &string1;
               char* string2 = &string2; 
               int insertAt = someInt;
    Then all you have to do is increment the pointers and remember that every string is terminated by the null character (\0) and remove the null character from string 2.

  6. #5
    Yuriy M's Avatar
    Yuriy M is offline Learning Programmer
    Join Date
    Sep 2007
    Location
    Winnipeg, Canada
    Posts
    70
    Rep Power
    0

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

    Quote Originally Posted by karthik
    Fantastic JOb!!
    this helped me to complete my project
    thank you very much
    Good to see that you're all smiles but don't go overboard. Part of the process of being a great programmer is coming up with various ways yourself to get your program working in the most efficient manner. Don't just copy and paste from forums.

    JCampos8782, thanks for the advice but the example you provided just gives me compile-time errors. I've already modified my program to have it provide the best result possible and it doesn't violate any C programming rules because it follows the guidelines that the author intended in my textbook.
    Last edited by Yuriy M; 03-01-2011 at 07:53 PM. Reason: Clarification
    For $1000: Something that is a miserable pile of secrets.

  7. #6
    rocketboy9000 is offline Learning Programmer
    Join Date
    Dec 2010
    Location
    Toronto
    Posts
    79
    Rep Power
    5

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

    Ok, but now try to insert the second string directly into the first, without creating a third.

  8. #7
    Yuriy M's Avatar
    Yuriy M is offline Learning Programmer
    Join Date
    Sep 2007
    Location
    Winnipeg, Canada
    Posts
    70
    Rep Power
    0

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

    Quote Originally Posted by rocketboy9000
    Ok, but now try to insert the second string directly into the first, without creating a third.
    When you mean by "without creating a third", do you mean outputting the results of two merged strings into a third string or inserting the contents of the second string into the first from a temporary storage string?
    For $1000: Something that is a miserable pile of secrets.

  9. #8
    Yuriy M's Avatar
    Yuriy M is offline Learning Programmer
    Join Date
    Sep 2007
    Location
    Winnipeg, Canada
    Posts
    70
    Rep Power
    0

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

    Ok, so after doing some fiddling, I was able to come up with a program that doesn't require the use of a third string in order to insert one string into another:

    Code:
    #include <stdio.h>
    
    main()
    {
          /* Declare variables */
          
          char string1[100],           /* The first string */
               string2[100],           /* The second string */
               *str_ptr1 = string1,    /* Pointer to the first string */
               *str_ptr2 = string2;    /* Pointer to the second string */
    
          int  position = 0,           /* 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(string1);
          
          /* 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(string2);
          
          /* Prompt the user for the position of the second string to be inserted */
          
          printf("\nEnter the 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 */        
          
          printf("\nHere is the result of your insertion: ");
          
          while (*str_ptr1 != '\0')
          {
           if (string_count == position)
           {
            while (*str_ptr2 != '\0')
            {
             putchar(*str_ptr2);
             str_ptr2++;
            }
           }
           putchar(*str_ptr1);
           str_ptr1++;
           string_count++;
          }
          printf("\n");
    }
    For $1000: Something that is a miserable pile of secrets.

  10. #9
    Xupicor's Avatar
    Xupicor is offline Learning Programmer
    Join Date
    Jan 2011
    Posts
    46
    Rep Power
    0

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

    int main()!
    By the way, do you find one space indenting readable?

  11. #10
    Yuriy M's Avatar
    Yuriy M is offline Learning Programmer
    Join Date
    Sep 2007
    Location
    Winnipeg, Canada
    Posts
    70
    Rep Power
    0

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

    The development environment that I use doesn't require the "int" data type for the main() function but thanks for noticing.

    And one space indenting is readable to me.
    For $1000: Something that is a miserable pile of secrets.

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 0
    Last Post: 10-01-2011, 01:45 PM
  2. Java | Converting String to Int & Int to String
    By QuackWare in forum Classes and Code Snippets
    Replies: 0
    Last Post: 01-06-2010, 11:01 AM
  3. C# trimming string or string manipulation
    By Siten0308 in forum C# Programming
    Replies: 3
    Last Post: 01-05-2010, 09:37 PM
  4. adding int to string,creat a file named string..
    By emda321 in forum C and C++
    Replies: 5
    Last Post: 09-10-2009, 07:15 AM
  5. cut last part of a string
    By Orjan in forum PHP Development
    Replies: 8
    Last Post: 11-17-2008, 11:15 AM

Tags for this Thread

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