Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

II.

Activity Section
String Activity 1

a. Problem
Write a C program that prints out the name of the school while
using the concepts in string.
b. Algorithm
Input: Enter school name (sname)
Process: -------
Output: Display school name(sname)
c. Flowchart

Figure 1. String Activity 1 Flowchart


The figure above indicates the path of the program. The program
starts with the declaration of a string. The program then prints out the
statement “School name” which is indicative of main objective of the
program. The program asks for the school name, denoted by the
variable sname. The program then prints out the inputted characters.
The program then comes to a stop.
d. Source Code

e. Program Output
String Activity 2

a. Problem
Write a C program that measure the length of the string of the
inputted favorite food of the user and two other favorite foods
(chicken adobo and chicken adobo written in single characters).
b. Algorithm
Input: Enter favorite food(favoritefood3)
Process: Measure length of strings favoritefood1, favoritefood2, and
favoritefood3.
Output: Display length of the strings favoritefood1, favoritefood2, and
favoritefood3.
c. Flowchart
Figure 2. String Activity 2 Flowchart
The figure indicated above shows the pathway the program must
follow to conduct the objectives. The program starts with the
declaration of variables used. This is followed by obtaining input from
the user, stored in the variable favoritefood3. The process in this
program involves the counting of the length of the strings
favoritefood1, favoritefood2, and favoritefood3. The program then
displays the data gathered from the process and ends afterwards.
b. Source Code

c. Program Output
String Activity 3

a. Problem
Make a C program that will print out the color of DLSU and the
term De La Salle University in the sequence De La Salle University,
De La Salle University, Green, Green given that a value, Ateneo de
University, is contained in the second variable. The program should
make use of strings and the strcpy() function.
b. Algorithm
Input: Enter the La Salle color (str4)
Process: Copy str1 to str 2 and str4 to str3
Output: Display the values of the strings (str1, str2, str3, str4)
c. Flowchart

Figure 3. String Activity 3 Flowchart


The figure above details the pathway the program must execute.
The program asks for an input from the user, str4. The process of the
program involves the copying of the content of a string to another. In
this case (str2 and str1) and (str3 and str4) are the variables included.
The program prints the new values to the output window and stops
afterwards.
d. Source Code

e. Program Output
String Activity 4

a. Problem
Write a C program that concatenates two variables, De La Salle
University and the department the user belongs to (str2).
b. Algorithm
Input: Enter department (str2)
Process: Concatenate str1 and str2
Output: Display str1
c. Flowchart

Figure 4. String Activity 4 Flowchart


The figure indicated shows the flowchart to be followed by the
program. The program first asks for an inputted value for the variable
str2. The program then concatenates str1 and str2. The program then
prints out the value of str1 to the output screen. The program stops
afterwards.
d. Source Code

e. Program Output
String Activity 5

a. Problem
Write a C program that will compare two strings by the amount of
characters. In this case, the first variable (str1) contains the favorite
cartoon character. The second variable (str2) contains the favorite
Marvel hero. The third variable (str3) contains the favorite DC hero,
and the fourth variable contains the favorite cartoon character.
b. Algorithm
Input: Enter favorite cartoon character(str1), favorite Marvel
hero(str2), and favorite DC hero (str3), and favorite cartoon character
(str4)
Process: Compare variables str1 and str2, str1 and str3, str1 and str4.
Output: Display results of the process
c. Flowchart
Figure 5. String Activity 5 Flowchart
The figure indicated above depicts the flow the program must
follow. The program must first ask for the following inputs from the
user: favorite cartoon character (str1), favorite Marvel hero(str2),
favorite DC hero(str3), and favorite cartoon character(str4). The
program stops afterwards.

d. Source Code

e. Program Output
String Activity 6

a. Problem
Write a C program that changes uppercased characters to
lowercased characters and vice versa. The inputted values are
favorite cartoon character in uppercase and favorite Marvel hero in
lowercase.
b. Algorithm
Input: Enter the favorite cartoon character in uppercase(str1) and the
favorite Marvel hero in lowercase(str2)
Process: Convert str1 to all lowercases and str2 to all uppercases
Output: Display the new values of str1 and str2
c. Flowchart
Figure 6. String Activity 6 Flowchart
The figure indicated above depicts the flow of the program to
execute the objectives of the program properly. The program first asks
for the favorite cartoon character of the user (str1) in uppercase and
then the favorite Marvel hero of the user (str2) in lowercase. The
program then modifies str1 to turn the characters in all lowercase
letters and outputs the new value to the output screen. The program
also modifies str2 to transform the characters to uppercases and then
outputs this value to the output screen. The program stops afterwards.

d. Source Code

e. Program Output
III. Supplementary Section
String Supplementary 1
a. Supplementary Worded Problem
Write a C program that transforms a regular singular noun to its
plural form and to show the plural form of an irregular noun. The
program must contain 10 irregular nouns.
b. Supplementary Program Algorithm
Input: Enter a singular noun (n) and a number between 1 to 10 (a)
Process: Add an s to the variable n. Manipulate characters in the
inputted value (a)
to transform the singular noun to its plural form
Output: Display n and value in the variable a
c. Supplementary Program Flowchart
Figure 7. String Supplementary 1 Flowchart
The figure above shows the pathway that must be followed in
writing the program. The program starts with asking for an input of a
singular noun denoted by the variable n. The program makes use the
function strcat() to transform the singular noun to its plural form. The
program then asks for a number between 1 to 10. The program must
execute the flow written in whatever value inputted by the user and
ends the program afterwards.
e. Supplementary Source Code
f. Supplementary Program Output

IV. Program Observation and Conclusion


The first activity delves into the use of strings in variable declaration and the
use of the functions puts and gets in lieu of printf and scanf. It can be noted that the
programmer only needs to put the character ‘s’ before the variable name to indicate
the utilization of a string. The statement gets() is used to obtain inputs from the user.
The statement puts(), on the other hand, is used to display the inputted value. The
use of printf and scanf for strings can still be used; however, gets and puts give the
programmer an easier time for input and output usage.
The second activity makes use of the function strlen() and the concepts used
in the first activity. The function strlen() measures the length of the string. By length,
this means the number of characters in the string. In this activity, the program will
measure the length of three strings where one of which is inputted by the user. The
function is denoted by strlen(string1) where the string indicated inside the function
will be computed for.
The third activity involves the function strcpy(). A string can be copied to
another string with this function. The function is written in the form strcpy(string1,
string2). String1 depicts the string that will be replaced. String2 is the string that will
be copied.
The fourth activity explores the function strcat(). With this, two strings are
concatenated in the form strcat(string1, string2). String2 will be attached to string1
and not vice versa. The output of this would be “string1string2”. It should be noted
that there is not space in between the two strings, and so a space after the
characters in string1 must be indicated if desired.
The fifth activity uses the function strcmp(). The task of the function is to
compare the length of two strings. The function is written as strcmp(string1,
string2). The function compares the total hexadecimal values of the characters in a
string and then compares this to the total hexadecimal value of the other string. A
value of -1, 0, and 1 will appear if the string1<string2, string1=string2, and
string1>string2 respectively.
The sixth activity entails the use of the functions strlwr() and strupr(). In this
activity, both functions were utilized and the input/output functions for strings too.
The function strlwr() transforms the characters in the string into lowercase letters.
The function strupr() on the other hand transforms the characters in the string into
uppercase letters. The programmer only needs to indicate the string inside the
function (e.g. strlwr(string1)) and the program will know the process to be done.
The supplementary activity makes use of strcat() and if-else statements
extensively. The goal of the program is to modify a string and print the initial value
and new value to the output window. In this case the string that will be modified will
pertain to a singular irregular noun. The function strcat() was used to modify the
string to add new characters and remove characters. The if-else statements were
used to determine which of the ten irregular nouns will the program showcase since
this requires an input from the user.
Experiment 6 explores strings. A string is a data type representing characters.
In the activities, it can be noted that the use of spaces when inputting characters is
permitted for the concept of strings are implemented in the program. Strings give
rise to the use of new functions such as puts(), gets(), strlen(), strcpy(), strcat(),
strcmp(), strlwr(), and strupr(). The previously mentioned functions, other than
puts() and gets(), are new concepts delved into since the concepts or idea of these
were not explored in the previous experiments.

You might also like