Submersed24 Submersed24 - 2 months ago 5
C Question

Program seems to be correct logically, cannot find out where it is wrong (Strings to uppercase)

I am trying to figure out why this program is not outputting correctly. It reads a string and outputs it in all caps, excluding the non alphabetical ones. It is skipping lines and I am trying to make 5 letters per word, newline after every 10 words. I realize there are other ways to do this program, but I am curious why this doesn't work. Thanks.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main(){
char str[100];
int c;
int i = 0;
puts( "Enter a sentence: " );
fgets( str, 100, stdin );

while( ( c = str[i] ) != EOF )
{
if(isalpha( c ) != 0)
{
putchar(toupper(c));
if( i % 5 == 0 && i != 0)
printf( " " );
if( i % 10 == 0 && i != 0)
printf( "\n" );
}
i++;
}
system("PAUSE");
return 0;
}

Answer

Change

if( i % 5 == 0 && i != 0);
                printf( " " );
if( i % 10 == 0 && i != 0);
                printf( "\n" );

To

if( i % 5 == 0 && i != 0)
                printf( " " );
if( i % 10 == 0 && i != 0)
                printf( "\n" );

Note the semi colon at the end of if statement is now gone.

Comments