0% found this document useful (0 votes)
356 views

Exercise String

The document contains examples and explanations of various string handling, character classification, and mathematical functions in C programming. It provides the syntax and usage of functions like strcpy(), strcat(), strcmp(), isalpha(), abs(), pow(), etc. It also includes programming exercises to practice using these functions, for example, checking if a string is a palindrome, counting words in a line, finding the longest common prefix of two words, replacing four-letter words in a string with asterisks, and forming plurals of nouns. Finally, it presents case studies on applications like determining relationship status from names using the FLAMES method and handling passwords securely.

Uploaded by

Tracy Loyola
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
356 views

Exercise String

The document contains examples and explanations of various string handling, character classification, and mathematical functions in C programming. It provides the syntax and usage of functions like strcpy(), strcat(), strcmp(), isalpha(), abs(), pow(), etc. It also includes programming exercises to practice using these functions, for example, checking if a string is a palindrome, counting words in a line, finding the longest common prefix of two words, replacing four-letter words in a string with asterisks, and forming plurals of nouns. Finally, it presents case studies on applications like determining relationship status from names using the FLAMES method and handling passwords securely.

Uploaded by

Tracy Loyola
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Exercise No.

4. What is string value in C?

A string in C is simply an array of characters.

5. Give the syntax of the following string functions: (string.h)

strcpy() - stpcpy(string1, string2) / strcpy(string1, string2)

strncpy() - strncpy(target, source, count)

strcat() - strcat(string1, string2)

strcmp() - strcmp(string1, string2)

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)

6. Explain the following character functions: (ctype.h)

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.

7. Give the syntax of the following mathematical functions: (math.h)

abs() – int abs(int num);

ceil() – double ceil(double num);

fabs() – syntax: double fabs(double num);

floor() – syntax: double floor(double num);

fmod()) – syntax: double fmod(double x, double y);

pow() – double pow(double base, double exp);

pow10() – syntax: double pow10(int n);

sqrt() – syntax: double sqrt(double num);

8. Explain the following conventional functions: (math.h)

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:

char first[20], second[15];

char third[20] = “God Loves U”;

char fourth[20] = “GOD BLESS U”;

a. strrev(fourth); = U SSELB DOG

b. strupr(third); = GOD LOVES YOU

c. strncat(fourth,third,5); = GOD BLESS UGod L

d. strlwr(fourth); = god bless you

e. strncpy(first,fourth,5); = God B

f. strcpy(second,third); = God Loves U

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:

char c = ‘C’, m = ‘?’, i = ‘t’, b = ‘5’;

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

3. Evaluate the following expressions:

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:

PROGRAMMING EXERCISES 7-1

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;

printf("Enter the string : ");

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,

two letters and so on.


PROGRAMMING EXERCISES 7-3

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”

is “depart”, and of “glove” and “dove” is the empty string);


PROGRAMMING EXERCISES 7-4

Write a program that takes a sequence of lines and displays each line with all four-letter

words replaced by asterisks.

#include <stdio.h>

#include <conio.h>

#include <string.h>

void main()

char str[100], ch;

char i;

clrscr();

printf("Enter string : ");

for(i=0; i<30; i++)

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:

a. if noun ends in “y”, remove the “y” and add “ies”

b. if noun ends in “s”, “ch”, or “sh” add “es”

c. in all other cases, just add “s”

Print each noun and its plural.

#include <stdio.h>

#include <string.h>

void pluralize (char word[]);

int main (void)

char noun[20];

printf("Enter a noun in singular form: ");

scanf("%s", noun);

pluralize (noun);

printf("The plural form is: %s\n", noun);

return;

void pluralize (char word[])

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';

else if (word[length - 1] == 's' ||

(word[length - 2] == 'c' && word[length - 1] == 'h') ||

(word[length - 2] == 's' && word[length - 1] == 'h'))

{ 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

equivalent using the game FLAMES!

Example

Name of Boy : John Victor Name of Girl : Vicenta Joy

Number of common letters Boy: 8 (J, o n,V,i,c,t,o)

Name of common letters Girl : 7 (V,i,c,n,t,J,o)

Total Number : 15 – Angry

#include <stdio.h>

#include <string.h>

void flames(char* a, char* b)

{ int i, j, k, l = 1, n, m, sc = 0, tc, rc = 0, fc = 5;

char q[25], w[25], c;

char f[] = "flames";

strcpy(q, a);

strcpy(w, b);

n = strlen(a);

m = strlen(b);

tc = n + m;

for (i = 0; i < n; i++) {

c = a[i];

for (j = 0; j < m; j++) {

if (c == b[j]) {

a[i] = -1;

b[j] = -1;

sc = sc + 2;

break;

} }

}
rc = tc - sc;

for (i = 0;; i++) {

if (l == (rc)) {

for (k = i; f[k] != '\0'; k++) {

f[k] = f[k + 1]; }

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");

else if (f[0] == 'f')

printf("FRIENDS");

else if (f[0] == 'm')

printf("MARRY");

else if (f[0] == 'l')

printf("LOVERS");

else if (f[0] == 'a')

printf("AFFECTION");

else

printf("SOULMATE");

int main()

char a[] = "alice";

char b[] = "alfred";

flames(a, b);

}
CASE STUDY 2 - PASSWORD

Write a program to input your password. Password must be invisible or may not be seen

by the user as you input your password.

#include <stdio.h>

#include <conio.h>

void main()

char password[30], ch;

int i;

clrscr();

printf("Enter password : ");

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

Rules for Pig Latin –

Each word is converted individually according to the following rules:

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")

You might also like