Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Code in C Done Lab9.docx Write a program that reads from the user some text in a string str that can hold a maximum of

Code in C image text in transcribed
Done Lab9.docx Write a program that reads from the user some text in a string str that can hold a maximum of 40 characters and then it splits the string even-position characters are copied into a string called even and all the odd-position characters are copied into a string called odd. The program should then print out the even and odd strings. Your program should use char * variables (pointer to char) to traverse the strings str, even and odd. The program should behave as follows (items underlined are into two other strings as follows: all the entered by the user) Enter a string (40 characters maximum) abcdefg The even string is: aceg The odd string is: bdif Algorithm design process . Declare your variables: a Strings str, even, odd: consider what the sizes should be (how many actual characters +the NULL terminator character). b. Pointers to char, which will traverse the above strings: p (for string str), e (for string even), o (for string odd). Another pointer p_start may be needed as discussed below 2. Read in the string from the user. You can use scanf with a specific conversion specifier that ensures we do not read more than 40 characters or you can use the code from the lecture notes . Write a for loop that uses a pointer that traverses the str string and copies the characters into the even and odd arrays. a How do we know if a character is on an even or odd position? Use pointer arithmetic: before the beginning of the for loop, initialize both p and p_start to the beginning of string str. Pointer p will be incremented at each iteration, while p_start will remain unchanged. If we compute pp start we get an even or odd number, depending on whether the character is on an even/odd position. We can then use an if-else statement to copy the character pointed to by p into the even/odd strings. Do not forget to increment the pointers that keep track of the current position in the even/odd strings b. At the end of the for loop do not forget to add the NULL terminator to the end of the even/odd strings. e. Print the even/odd strings using printf

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

4th Edition

0805360476, 978-0805360479

More Books

Students also viewed these Databases questions