1.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char ch;
int sc = 0,nc = 0,oc = 0;
printf("Eenter something you want to be statisticed(\"#\" to quit):");
while ((ch = getchar()) != '#')
{
if (ch == ' ')
sc++;
else if (ch == '\n')
nc++;
else
oc++;
}
printf("There are %d space, %d newlines,%d others\n",sc,nc,oc);
return 0;
}
2.
#include <stdio.h>
int main(void)
{
char ch;
int n = 0;
printf("Enter something(# to quit: ");
while ((ch = getchar()) != '#')
{
if ((n % 8 == 0) && n != 0)
printf("\n");
printf("%c-%d,",ch,ch);
n++;
}
return 0;
}
3.
#include <stdio.h>
int main(void)
{
int num;
int ct_even = 0, ct_odd = 0;
double sum_even = 0.0,sum_odd = 0.0;
printf("Enter a integer: ");
while (scanf("%d",&num) == 1 && num != 0)
{
if (num % 2 == 0)
{
ct_even++;
sum_even += (float)num;
}
else
{
ct_odd++;
sum_odd += (float)num;
}
printf("Enter the next integer(0 to quit): ");
}
printf("Number of evens: %d",ct_even);
if (ct_even > 0)
printf(" average: %g",sum_even / ct_even);
printf("\n");
printf("Number of odds: %d",ct_odd);
if (ct_odd > 0)
printf(" average: %g",sum_odd / ct_odd);
printf("\n");
printf("done\n");
return 0;
}
4.
#include <stdio.h>
int main(void)
{
char ch;
int i = 0;
int j = 0;
printf("Enter something to test the program(# to quit): ");
while ( (ch = getchar()) != '#')
{
if ('.' == ch)
{
i++;
putchar('!');
}
else if ('!' == ch)
{
putchar('!');
putchar('!');
j++;
}
else
putchar(ch);
}
printf("%d times",i + j);
return 0;
5.
#include <stdio.h>
int main(void)
{
char ch;
int ct1 = 0;
int ct2 = 0;
printf("Enter something to test the program(# to quit): ");
while ( (ch = getchar()) != '#')
{
switch (ch)
{
case '.':
putchar('!');
ct1++;
break;
case '!':
putchar('!');
putchar('!');
ct2++;
break;
default:
putchar(ch);
}
}
printf("%d times",ct1 + ct2);
return 0;
}
6.
#include <s