Exercise String
Exercise String
stricmp() - stricmp(string1,string2)
strncmp() - strncmp(string1,string2,count)
strnicmp() - strnicmp(string1,string2,count)
strlwr() - strlwr(string)
strupr() - strupr(string)
strnset() - strnset(string,c,count)
strset() - strset(string,ch)
strchr() - strchr(string, c)
strlen() - strlen(string)
strrev() - strrev(string)
strdup() strdup(string)
isalnum() - The isalnum() function returns non-zero if its argument is either a letter of the alphabet or a
digit.
isalpha() - The isalpha() function returns non-zero if ch is a letter of the alphabet otherwise zero is
returned.
isdigit() - The isdigit() function returns non-zero if ch is a digit, that is 0-9, otherwise zero is returned.
islower() - The islower() function returns non-zero if ch is a lowercase letter(a-z); otherwise zero is
returned.
ispunct() - The ispunct() function returns non-zero if ch is a punctuation character, excluding the space;
otherwise zero is returned.
isspace() - The isspace() function returns non-zero if ch is one of the following: a space, tab, vertical tab,
form feed, carriage return, newline; otherwise zero is returned.
tolower() - The tolower() function returns the lowercase equivalent of ch if ch is a letter, otherwise, ch is
returned unchanged.
toupper() - The toupper() function returns the uppercase equivalent of ch, if c is a letter, otherwise ch is
returned unchanged.
atof() – The atof() function convert the string pointed to be str into a double value.
atoi() – The atoi() function converts the string pointed to by str into an int value. The string must contain
valid int value, if not zero is returned.
atol() – The atol() function converts the string pointed to by str into a long int value. The string must
contain valid long int value.
itoa() – The itoa() function converts the integer number into string equivalent and places the result in
the string pointed to be str. The base of the output string is determined by radix which can be in the
range of 2 – 36. This function returns a pointer to str. There is no error return value. Be sure to call itoa()
with a string of sufficient length to hold the converted result. The maximum length needed is 17 bytes
1. Evaluate the following expression using the following:
e. strncpy(first,fourth,5); = God B
g. strlen(third); = 10
h. strncat(third,fourth,4); = strncat(third,fourth,4);
i. strlen(third); = 11
j. strncpy(first,third,3); = GOD
2. Answer TRUE if the expression will return non-zero and FALSE if not. Evaluate using the
following declarations:
a. isdigit(b); = TRUE
b. isalpha(c); = TRUE
c. isspacee(m); = FALSE
d. isupper(c); = FALSE
e. isalnum(b); = TRUE
f. ispunct(m); = TRUE
g. islower(i); = TRUE
h. isupper(c); = FALSE
i. isalnum(b); = TRUE
j. islower(i); = TRUE
a. abs(5); = 5
b. floor(5.5); = 5
c. ceil(5); = 5
d. fmod(pow(7,2));
e. sqrt(floor(25.12)); = 5
f. fabs(pow(9,2)); = 81
g. atoi(“451”); = 451
h. ceil(pow(5,3)); = 75
i. fabs(-44.98); = 44
j. ceil(fmod(5,1,5)); =
Activities/Assessments:
A palindrome is a string that reads the same both forward and backward. Some examples are:
“ABCBA”. “otto”, “I am ma I” Write a function that takes a string as an argument and returns the int
value 1 if the string is a palindrome and returns 0 otherwise.
#include <stdio.h>
#include <string.h>
int main()
char s[1000];
int i,n,c=0;
gets(s);
n=strlen(s);
for(i=0;i<n/2;i++)
if(s[i]==s[n-i-1])
c++;
if(c==i)
printf("1");
else
printf("0");
return 0;
}
PROGRAMMING EXERCISES 7-2
Write a program that processes a sequence of lines, displaying a count of the total
number of words in those line as well as counts of the number of words with one letter,
Write and test a function that finds the longest common prefix of two words (e.g. the
longest common prefix of “global” and “glossary” is “glo”, of “department” and “depart”
Write a program that takes a sequence of lines and displays each line with all four-letter
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
char i;
clrscr();
ch = getch();
str[30] = ch;
strnset(str,'*',4);
printf("%s",ch);
str[30] = "";
}
PROGRAMMING EXERCISES 7-5
Write a program that takes nouns and forms their plurals on the basis of these rules:
#include <stdio.h>
#include <string.h>
char noun[20];
scanf("%s", noun);
pluralize (noun);
return;
int length;
char noun;
length=1;
length = strlen(word);
if (word[length - 1] == 'y')
{ word[length - 1] = 'i';
word[length] = 'e';
word[length + 1] = 's';
word[length + 2] = '\0';
{ strcat(word, "es");
else
{ strcat(word, "s");
return;
}
CASE STUDY 1 - FLAMES
Write a program to ask the user to input the name of boy and name of girl. Count the
number of common letters to their names and add them up. Determine the corresponding
Example
#include <stdio.h>
#include <string.h>
{ int i, j, k, l = 1, n, m, sc = 0, tc, rc = 0, fc = 5;
strcpy(q, a);
strcpy(w, b);
n = strlen(a);
m = strlen(b);
tc = n + m;
c = a[i];
if (c == b[j]) {
a[i] = -1;
b[j] = -1;
sc = sc + 2;
break;
} }
}
rc = tc - sc;
if (l == (rc)) {
f[k + 1] = '\0';
fc = fc - 1;
i = i - 1;
l = 0;
} if (i == fc) {
i = -1;
if (fc == 0) {
break;
} l++; }
if (f[0] == 'e')
printf("ENEMIES");
printf("FRIENDS");
printf("MARRY");
printf("LOVERS");
printf("AFFECTION");
else
printf("SOULMATE");
int main()
flames(a, b);
}
CASE STUDY 2 - PASSWORD
Write a program to input your password. Password must be invisible or may not be seen
#include <stdio.h>
#include <conio.h>
void main()
int i;
clrscr();
for(i=0;i<100;i++)
ch = getch();
password[30] = ch;
ch = "" ;
printf("%c",ch);
password[30] = "";
}
CASE STUDY 3 - PASSWORD + PIG LATIN
Write a program to input your password. Password must be invisible or may not be seen
by the user as you input your password. If the password is correct, call function
Pig_Latin_Converter
1. If the word has no vowels (other than 'y', e.g. "my", "thy") append "yay" to it -- i.e.,
"myyay", "thyyay".
2. If the word begins with a vowel (e.g., "art", "else") append "yay" to the word (i.e.,
"artyay", "elseyay").
3. If the word begins with a consonant (e.g., "song", "pig") divide the word at the first
vowel, swapping the front and back halves and append "ay" to the word (i.e., "ongsay",
"igpay")