Solution for Exercise 17-7
ex1707
#include <stdio.h> #include <ctype.h> int main() { char input[64]; int ch; int x = 0; printf("Type in some text: "); fgets(input,63,stdin); while(input[x] != '\n') { ch = input[x]; if(isalpha(ch)) ch &= 223; putchar(ch); x++; } putchar('\n'); return(0); }
Output
Type in some text: Math is like 8*8=64
MATH IS LIKE 8*8=64
Notes
* For my solution, I took refuge in the function isalpha() in Line 15. That test returns TRUE whenever a letter of the alphabet is encountered, FALSE otherwise.
* Remember that you need to include the ctype.h
header file when you use the isalpha() function.
* Did you see on Line 17 an assignment operator is used? The &=
operator allows you to write:
c &= 223;
Instead of:
c = c & 223;
* A non-CTYPE solution could be the following if statement:
if( ch>='a' && ch<='z' )
Because the bitwise AND operator affects primarily lowercase letters, only they are singled out in the if statement's expression.
Copyright © 1997-2024 by QPBC.
All rights reserved