//done in C please Write a program that reads from the user some text in a strin
ID: 3714584 • Letter: #
Question
//done in C please
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 into two other strings as follows: all the 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 in italics are entered by the user):
Enter a string (40 characters maximum): abcdefg
The even string is: aceg
The odd string is: bdf
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 into two other strings as follows: all the 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 entered by the user) Enter a string (40 characters maximum): abcdefg The even string is: aceg The odd string is: bdfExplanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
#include <stdio.h>
int main()
{
char str[41];
char odd[21], even[21];
char *p, *p_start, *o, *e;
int i;
printf("Enter a string (maximum 40 characters): ");
scanf("%40s", str);
p = p_start = str;
o = odd;
e = even;
while(*p != '')
{
i = p - p_start;
if(i % 2 == 0) //even
{
*e = *p;
e++;
}
else
{
*o = *p;
o++;
}
p++;
}
*e = '';
*o = '';
printf("The even string is: %s ", even);
printf("The odd string is: %s ", odd);
}
output
----
Enter a string (maximum 40 characters): abcdefg
The even string is: aceg
The odd string is: bdf
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.