CS 1050 University of Missouri Columbia Pointers and Strings Program Lab

User Generated

IvpxlObo

Programming

CS 1050

University of Missouri Columbia

CS

Description

Description

For the lab assignment, you need to implement a program that takes an input string and divides it into segments.Each segment will be 3 characters long (or less if you run out of string).The program will then reverse the order of the segments in-place in the original string.For example, if the original string was “zyxwvuts” the resultant string should be “stuvwxyz”.That is, you can think of the original string as 3 segments of “zyx”, “wvu”, and “ts”.Reversing the order of these segments you get “ts”, “wvu”, and “zyx” or a full result of “tswvuzyx”.

You may not call any function in string.h from the Standard C Library.You must use pointer arithmetic to move through any strings in the program – not the index operator [].You may not use any global variables at all.

The main() function in your program should:

  • Print a message welcoming the user to Lab 9.
  • Call a function (written by you) to get an original string from the user.
  • Print the original string.
  • Call a function (written by you) to determine how many segments are in the original string.
  • Print how many segments are in the original string.
  • Call a function (written by you) to reverse the order of the segments of the original string in place.
  • Print the resultant string (the modified original string).
  • Print a message thanking the user.

Note that you may assume that the original string will not be larger than 247 characters (248 with space for the null-terminator).


Functions You Must Write

You may write any functions you wish to implement this program.Here is some advice

  • You need a function that will get a string from the user.
  • You need a function to calculate how many segments are in the original string.
  • You need a function that reverses the order of the segments.
  • int main(void) – Of course, you need to write a main() 😊.

Hints

  • Set a pointer to the last segment in the string, and work backward.
  • Have a temporary string where you can put each segment as you find it.
    • So, for the test string “zyxwvuts” your temporary string would change as follows
      • “” (empty to begin with)
      • “ts” (now it has just the last segment in it)
      • “tswvu” (now it has 2 segments in it)
      • “tswvuzyx” (now it has all 3 segments in it)
  • If you use a temporary string, you can just copy it over the top of the original string at the end.
  • To debug this, load your code with printf() calls!The easier it is to see what is going on (or going wrong) in your code, the easier it is to make things work correctly.

Unformatted Attachment Preview

CS1050 – Lab 9 Fall 2020 Concepts to Practice • • • Pointers Strings Expand the prelab Submission Information Submit this assignment by following the instructions given by your TA. SUBMIT ONLY the .c file (no a.out or executable file is required). All of the lab assignments must be submitted before the end of the lab using the lab code given by the TA. Use the following submit command: mucs submit For example: mucs submit 1050 lab m-lab9.c Description For the lab assignment, you need to implement a program that takes an input string and divides it into segments. Each segment will be 3 characters long (or less if you run out of string). The program will then reverse the order of the segments in-place in the original string. For example, if the original string was “zyxwvuts” the resultant string should be “stuvwxyz”. That is, you can think of the original string as 3 segments of “zyx”, “wvu”, and “ts”. Reversing the order of these segments you get “ts”, “wvu”, and “zyx” or a full result of “tswvuzyx”. You may not call any function in string.h from the Standard C Library. You must use pointer arithmetic to move through any strings in the program – not the index operator []. You may not use any global variables at all. The main() function in your program should: 1. 2. 3. 4. 5. 6. Print a message welcoming the user to Lab 9. Call a function (written by you) to get an original string from the user. Print the original string. Call a function (written by you) to determine how many segments are in the original string. Print how many segments are in the original string. Call a function (written by you) to reverse the order of the segments of the original string in place. 7. Print the resultant string (the modified original string). 8. Print a message thanking the user. Note that you may assume that the original string will not be larger than 247 characters (248 with space for the null-terminator). Functions You Must Write You may write any functions you wish to implement this program. Here is some advice • • • • You need a function that will get a string from the user. You need a function to calculate how many segments are in the original string. You need a function that reverses the order of the segments. int main(void) – Of course, you need to write a main() . Hints • • • • Set a pointer to the last segment in the string, and work backward. Have a temporary string where you can put each segment as you find it. o So, for the test string “zyxwvuts” your temporary string would change as follows ▪ “” (empty to begin with) ▪ “ts” (now it has just the last segment in it) ▪ “tswvu” (now it has 2 segments in it) ▪ “tswvuzyx” (now it has all 3 segments in it) If you use a temporary string, you can just copy it over the top of the original string at the end. To debug this, load your code with printf() calls! The easier it is to see what is going on (or going wrong) in your code, the easier it is to make things work correctly. Sample Output (5 different runs) jimr@JimRArea51:~/CS1050/FS2020/labs/lab9$ ./a.out *** Welcome to Lab 9 *** Please enter a string: zyxwvuts You entered: zyxwvuts There are 3 segments in the string. The modified string is: tswvuzyx *** Thanks for doing Lab 9 *** jimr@JimRArea51:~/CS1050/FS2020/labs/lab9$ ./a.out *** Welcome to Lab 9 *** Please enter a string: a You entered: a There are 1 segments in the string. The modified string is: a *** Thanks for doing Lab 9 *** jimr@JimRArea51:~/CS1050/FS2020/labs/lab9$ ./a.out *** Welcome to Lab 9 *** Please enter a string: abc You entered: abc There are 1 segments in the string. The modified string is: abc *** Thanks for doing Lab 9 *** jimr@JimRArea51:~/CS1050/FS2020/labs/lab9$ ./a.out *** Welcome to Lab 9 *** Please enter a string: abcd You entered: abcd There are 2 segments in the string. The modified string is: dabc *** Thanks for doing Lab 9 *** jimr@JimRArea51:~/CS1050/FS2020/labs/lab9$ ./a.out *** Welcome to Lab 9 *** Please enter a string: JimRiesIsCool You entered: JimRiesIsCool There are 5 segments in the string. The modified string is: lCoosIsRieJim *** Thanks for doing Lab 9 *** Guidelines for Grading Lab 9 40 Points Possible General If your program does not compile or produce any input/output (I/O) because most of the source code is commented out then your lab will receive a grade of ZERO POINTS. Further, if your program does not actually follow the specifications, but merely prints out lines that make it appear to follow the specifications, you will receive a grade of ZERO POINTS. For partial credit your C program must not only compile but also produce some valid I/O that meets the lab specifications. You program is expected to have a comment header at the top that includes your name, pawprint, the course you are taking, and the lab that you are solved (e.g., “Lab 9”). Your code should be nicely indented. You will lose up to 10 points if you do not meet these basic requirements. If you use a function from string.h and/or you use the index operator [], you will lose 50% of the points you would have otherwise received. If you use a global variable you will get zero points. 5 points: Program correctly allows string input from a user-written function. 5 points: Program correctly prints the string input by the user. 10 points: Program correctly calculates the number of segments the string. 15 points: Program correctly reverses the order of each segment of the string. 5 points: Program uses the original string in main() as the reversed string at the end.
Purchase answer to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

Attached. Pleas...


Anonymous
I was stuck on this subject and a friend recommended Studypool. I'm so glad I checked it out!

Studypool
4.7
Indeed
4.5
Sitejabber
4.4

Related Tags