Within the for loop an output of the new line character '\n'
is executed after printing each character in the string that is not equal to '\t'
or to ' '
.
if(a[i] != '\t' || a[i] != ' '){
printf("%c", a[i]);
}
putchar('\n');
Moreover the condition in the if statement shall be written at least like
if(a[i] != '\t' && a[i] != ' '){
^^^^
You need to output the new line character after a whole name is outputted. So if you are outputting a name character by character then you need one more inner loop.
Also the header <string.h>
is redundant because neither declaration from the header is used in your program. So you may remove the directive
#include <string.h>
The algorithm can look the following way as it is shown in the demonstrative program below. It is implemented as a separate function.
#include <stdio.h>
void print_names( const char *s )
{
while ( *s != '\0' )
{
while ( *s == ' ' || *s == '\t' ) ++s;
while ( *s && *s != ' ' && *s != '\t' ) putchar( *s++ );
putchar( '\n' );
}
}
int main(void)
{
char a[] = "Bill Sarah Alice";
print_names( a );
return 0;
}
The program output is
Bill
Sarah
Alice
If to include the header <ctype.h>
#include <ctype.h>
then the function can be rewritten using the standard C function isblank
that itself does the check whether a character is equal to the space character or to the tab character.
#include <stdio.h>
#include <ctype.h>
void print_names( const char *s )
{
while ( *s != '\0' )
{
while ( isblank( ( unsigned char )*s ) ) ++s;
while ( *s && !isblank( ( unsigned char )*s ) ) putchar( *s++ );
putchar( '\n' );
}
}
//...