Exercise 4 [Arrays and Strings] Write a function Split that takes as argument a
ID: 3837386 • Letter: E
Question
Exercise 4 [Arrays and Strings]
Write a function Split that takes as argument a C string (terminated with ‘’) that contains a single occurrence of the ‘@’ character. Your function should print the two substrings separated by ‘@’ on separate lines, using the “%s” format specifier for printf. (You may not print individual characters with putchar or “%c”.)
Here are two examples showing the function behavior:
Function Call
Output
Split("place@here");
place
here
Split("do@that");
do
that
void Split(char * s)
{
/* Add your code here */
Function Call
Output
Split("place@here");
place
here
Split("do@that");
do
that
Explanation / Answer
#include<stdio.h>
//function will print the two substrings separated by ‘@’ on separate lines, using the “%s” format specifier for printf.
void Split(char *s)
{
int i;
//finding the @character
for(i=0;s[i]!='@';i++);
char c[i];
//spliting first string
for(i=0;s[i]!='@';i++)c[i]=s[i];
//printing strings...in two lines..
printf("%s %s ",c,&s[i+1]);
return;
}
int main()
{
Split("do@that");
Split("place@here");
return 0;
}
output:-
do
that
place
here
Process exited normally.
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.