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:
Here is the program that I've written up: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".
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?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"); }
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?


LinkBack URL
About LinkBacks





Reply With Quote

Bookmarks