100% found this document useful (1 vote)
187 views14 pages

C String and Character Functions Guide

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
187 views14 pages

C String and Character Functions Guide

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
  • Programming Exercises
  • Case Studies

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

Common questions

Powered by AI

The `itoa()` function converts an integer to a string representation in a specified base, ensuring that the result fits into the buffer provided. The buffer must be long enough to hold the longest possible string, including the null terminator, which for a 32-bit integer and a base of 10, can require up to 17 bytes of space. Incorrect buffer sizing can lead to buffer overflows .

The method uses `getch()` to read characters without echoing them to the screen, providing invisibility. However, this approach does not secure the input from being accessed in memory, and it lacks basic protection against buffer overflow as the code suggests writing beyond bounds of `password[30]` when using a loop up to 100 .

The `strlwr()` function converts all the characters of a string to lowercase, whereas `strupr()` converts all characters to uppercase. When `strupr()` is applied to the string `third` initialized as "God Loves U", it transforms it into "GOD LOVES U" .

In C, the `strlen()` function is used to determine the length of a string, which returns the number of characters before the null terminator. For `third` initialized as "God Loves U", calling `strlen(third)` results in the length 10 since it counts all characters including spaces but excluding the null terminator .

The `strcat()` function concatenates one string to the end of another without regard to length specifications, while `strncat(target, source, count)` appends up to `count` characters from the source string to the target string. In `strncat(fourth, third, 5)`, it appends the first 5 characters from `third` ("God L") to `fourth` ("GOD BLESS U"), resulting in "GOD BLESS UGod L" .

The function processes lines by splitting them into words and categorizing each based on its length. This is done using delimiters such as spaces and measuring the length of each resultant substrings. Applications of this method include text analysis to determine text complexity, readability statistics, or filtering texts by word size .

The pluralization logic applies different rules: if a noun ends with 'y', change it to 'ies'; if ending in 's', 'ch', or 'sh', add 'es'; otherwise, just add 's'. Potential edge cases that might not be handled include irregular nouns like "mouse" to "mice", which require exceptions outside of conventional rule-based approaches .

The `flames` program removes all common letters between two names and uses the count of remaining letters to eliminate characters from the string "flames". Each remaining character corresponds to a relationship outcome such as "FRIENDS" or "LOVERS". The process symbolizes the nature of relationships based on shared characteristics .

The palindrome function compares characters from the beginning and end of the string, moving towards the center, and if all pairs match, it returns 1, indicating a palindrome. To improve, it could ignore spaces and consider case insensitivity by using `tolower()` on characters during the comparison to increase its robustness .

The `strrev()` function reverses the string on which it is called. When applied to the string `fourth` with the value "GOD BLESS U", the function will reverse its characters, resulting in the string "U SSELB DOG" as the outcome .

Exercise No. 7
4. What is string value in C?
A string in C is simply an array of characters.
5. Give the syntax of the follow
ispunct() - The ispunct() function returns non-zero if ch is a punctuation character, excluding the space; 
otherwise zero is
1. Evaluate the following expression using the following:
char first[20], second[15];
char third[20] = “God Loves U”;
char fo
g. islower(i); = TRUE
h. isupper(c); = FALSE
i. isalnum(b); = TRUE
j. islower(i); = TRUE
3. Evaluate the following expression
Activities/Assessments:
PROGRAMMING EXERCISES 7-1
A palindrome is a string that reads the same both forward and backward. Som
PROGRAMMING EXERCISES 7-2
Write a program that processes a sequence of lines, displaying a count of the total
number of words
PROGRAMMING EXERCISES 7-3
Write and test a function that finds the longest common prefix of two words (e.g. the
longest commo
PROGRAMMING EXERCISES 7-4
Write a program that takes a sequence of lines and displays each line with all four-letter
words re
PROGRAMMING EXERCISES 7-5
Write a program that takes nouns and forms their plurals on the basis of these rules:
a. if noun en
if (word[length - 1] == 'y') 
 {   word[length - 1] = 'i';
     word[length] = 'e';
    word[length + 1] = 's';
    word[le

You might also like