Sunteți pe pagina 1din 4

Exercise 1: A string is said to be a palindrome if it reads the same both from the left and from the right.

Examples of palindromes are: mom, Mom, dad, Dad, madam, SMs, etc. Write a function, int ispalindrome(char s[]); that takes a string and returns 1 true! if the string is a palindrome and " other#ise. Write a main function to test the program.

#include <stdio.h> #include <string.h> #include <ctype.h> int isPalindrome (char s[]); int main() { char string[50]; int i; print (!"nter your string# >!); gets(string); i (isPalindrome(string)) print (!$he string% &s% is palindrome!%string); else print (!$he string%&s% is not a palindrome!%string); return 0; ' int isPalindrome (char s[]) { int i%l(strlen(s); or(i(0;i<l)*;i++) { i (tolo,er(s[i])-(tolo,er(s[l./.i])) return 0; ' return /; '

Exercise 2: Write a function, $oid remo$e%nonalnum char s&'!( that remo$es all non) alphanumeric characters and spaces from the string, s. Write a main function to test it.

*include +stdio.h, *include +string.h, *include +ct-pe.h, $oid remo$e%nonalnum char s&'!( $oid remo$e. char s&'!( int main ! / char string&0"'( int i( printf 1Enter -our string: ,1!( gets string!( remo$e. string!( printf 1After remo$ing non alphanum characters2n1!( printf 13s1,string!( return "( 4 $oid remo$e%nonalnum char s&'!/ int i,5,l6strlen s!( for i6"(s&i'7682"8(! / if 7isalnum s&i'!!/ for 56i(5+l(599! s&5'6s&591'( 4 else i99( 4 4 $oid remo$e. char s&'!/ int i,56"( for i6"(s&i'7682"8(i99! / if isalnum s&i'!!
*

s&599'6s&i'( 4 s&5'682"8( 4 Exercise 3: :he text file, ;input.txt<, contains some text. Write a program that counts ho# man- times a certain #ord is repeated. :he #ord is entered b- the user. =our program must be case insensitive. Hint: =ou need to use the strtok function to tokeni>e each line of the text using space and punctuation characters as delimiters. =ou also need to #rite a function to con$ert a string to lo#ercase or uppercase.
*include +stdio.h, *include +string.h, *include +ct-pe.h, *define S?@E A" $oid stolo#er char s&'! / int i( for i6"(s&i'7682"8(i99! / s&i'6tolo#er s&i'!( 4 4 int main ! / int count6"( char line&S?@E',#ord&S?@E',Bstatus,Btoken( char delim&'61 ,(:C72n1( D?EE Bin( in6fopen 1smile.txt1,1r1!( printf 1Enter a #ord to count,1!( gets #ord!( stolo#er #ord!( status6fgets line,S?@E,in!( #hile status76FGEE! / stolo#er line!( token6strtok line,delim!( #hile token76FGEE! / if strcmp token,#ord!66"! count99( token6strtok FGEE,delim!( 0

4 status6fgets line,S?@E,in!( 4 fclose in!( printf 13s is repeated 3d times1,#ord,count!( return "( 4

S-ar putea să vă placă și