1.
#include<stdio.h>
int main(void)
{
int count = 0;
char ch;
while (1)
{
ch = getchar();
if (ch == '\n')
continue;
else if (ch == EOF)
break;
else
count++;
}
printf("%d", count);
return 0;
}
2.
#include <stdio.h>
int main(void)
{
char ch;
int i;
for (i = 1; (ch = getchar()) != EOF; i++)
{
if (ch >= ' ' || ch == '\n' || ch == '\t') printf("%-5c", ch);
else printf("^%-4c", ch + 64);
printf("%-5d", ch);
if (i % 8 == 0) printf("\n");
}
return 0;
}
3.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char ch;
int lower = 0, upper = 0;
while ((ch = getchar()) != EOF)
{
if (islower(ch)) lower++;
if (isupper(ch)) upper++;
}
printf("lower: %d, upper: %d\n", lower, upper);
return 0;
}
4.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int num = 0;
char ch;
bool word = 0;
while ((ch = getchar()) != EOF)
{
if (isalpha(ch) && !word)
{
word = 1;
num++;
}
if (!isalpha(ch))
{
word = 0;
}
}
printf("words: %d\n", num);
return 0;
}
5.
#include <stdio.h>
int main(void)
{
int guess, max = 100, min = 1;
char response;
printf("Pick an integer from 1 to 100.I will try to guess ");
printf("it.\nRespond with a b if my quess is big and with");
printf("\nan l if it is little.\n");
printf("Also,Respond a y if it is right.\n");
printf("Uh...is your number %d?\n", guess = (max + min) / 2);
while ((response = getchar()) != 'y')
{
if (response == 'b')
{
max = guess - 1;
printf("Well,then,is it %d?\n", guess = (max + min) / 2);
}
else if (response == 'l')
{
min = guess + 1;
printf("Well,then,is it %d?\n", guess = (max + min) / 2);
}
else printf("Sorry,I understand only y or n.\n");
while (getchar() != '\n');
}
printf("I know I could do it!\n");
return 0;
}
6.
#include <stdio.h>
#include <ctype.h>
char get_first(void);
int main(void)
{
char ch;
while ((ch = get_first()) != EOF)
{
putchar(ch);
}
return 0;
}
char get_first(void)
{
int ch;
while (isspace(ch = getchar()));
while (getchar() != '\n');
return ch;
}
7.
#include<stdio.h>
#include<ctype.h>
char get_first(void);
#define TIME 40
#define ADD 1.5
#define LIMIT1 300
#define RATE1 0.15
#define LIMIT2 150
#define RATE2 0.20
#define RATE3 0.25
int main(void)
{
double basic, hours, gross, tax;
printf("Enter the number corresponding to the desired pay rate or action:\n");
printf("1) $8.75/hr\t\t\t2) $9.33/hr\n");
printf("3) $10.00/hr\t\t\t4) $11.20/hr\n");
printf("5) quit\n");
switch (get_first())
{
case '1': basic = 8.75; break;
case '2': basic = 9.33; break;
case '3': basic = 10.00; break;
case '4': basic = 11.20; break;
default: printf("quit\n"); return 0;
}
printf("you have select $%.2lf\n", basic);
printf("input the work hours of a week:");
scanf("%lf", &hours);
if (hours > 40) hours = 40 + (hours - 40) * 1.5;
gross = hours * basic;
printf("gross income:\t\t%lf\n", gross);
if (gross <= LIMIT1) tax = gross * RATE1;
else if (gross <= LIMIT2) tax = LIMIT1 * RATE1 + (gross - LIMIT1) * RATE2;
else tax = LIMIT1 * RATE1 + LIMIT2 * RATE2 + (gross - LIMIT1 - LIMIT2) * RATE3;
printf("tax:\t\t\t%lf\n", tax);
printf("net income:\t\t%lf\n", gross - tax);
return 0;
}
char get_first(void)
{
int ch;
while (isspace(ch = getchar()));
while (getchar() != '\n');
return ch;
}
8.
#include<stdio.h>
#include<ctype.h>
float get_float(void);
char get_first(void);
int main(void)
{
char select;
float num1, num2;
while (1)
{
printf("Enter the operation of your choice:\n");
printf("a.add s.subtract:\n");
printf("m.multiply d.divide\n");
printf("q.quit\n");
select = get_first();
if (select != 'a' && select != 's' && select != 'm' && select != 'd')
{
printf("Bye.\n");
break;
}
printf("Enter first number:");
num1 = get_float();
printf("Enter second number:");
num2 = get_float();
while (select == 'd' && num2 == 0)
{
printf("Enter a number other than 0:");
num2 = get_float();
}
switch (select)
{
case 'a': printf("%.2f + %.2f = %.2f\n", num1, num2, num1 + num2); break;
case 's': printf("%.2f - %.2f = %.2f\n", num1, num2, num1 - num2); break;
case 'm': printf("%.2f * %.2f = %.2f\n", num1, num2, num1 * num2); break;
case 'd': printf("%.2f / %.2f = %.2f\n", num1, num2, num1 / num2); break;
default: break;
}
}
return 0;
}
float get_float(void)
{
float num;
char str[40];
while (scanf("%f", &num) != 1)
{
gets(str);
printf("%s is not a number.\n", str);
printf("Please enter a numbe, such as 2.5, -1.78E8, or 3:");
}
while (getchar() != '\n');
return num;
}
char get_first(void)
{
int ch;
while (isspace(ch = getchar()));
while (getchar() != '\n');
return ch;
}