You are on page 1of 17

COLLEGE OF COMPUTER STUDIES

CCS007L
(COMPUTER PROGRAMMING 2)

EXERCISE

3
CHARACTER AND STRING MANIPULATION

Student Name / Group


Name:
Name Role
Members (if Group):

Section:

Professor:
I. PROGRAM OUTCOME/S (PO) ADDRESSED BY THE LABORATORY EXERCISE
 Apply knowledge through the use of current techniques and tools necessary for the IT
profession. [PO: I]

II. COURSE LEARNING OUTCOME/S (CLO)ADDRESSED BY THE LABORATORY EXERCISE


 Adapt and apply appropriate techniques, resources, and modern computing tools to
create computing applications [CLO: 3]

III. INTENDED LEARNING OUTCOME/S (ILO) OF THE LABORATORY EXERCISE


At the end of this exercise, students must be able to:
 Create a program that applies different C-String functions
 Create a program process C-String values using user-defined functions

IV. BACKGROUND INFORMATION


A string stored as an array of characters terminated with ‘\0’.

Table 1: Some Predefined C-String Functions in <cstring>


FUNCTION DESCRIPTION
strcpy(Target_String_Var,Src_String) Copies the C-string value Src_String into the C-string
variable Target_String_Var
strcpy(Target_String_Var,Src_String,limit) The same as the two argument strcpy except that at most
Limit characters are copied
strcat(Target_String_Var,Src_String) Concatenates the C-String value Src_String onto the end of
C-string in the C-string variable Target_String_Var
strcat(Target_String_Var,Src_String, The same as the two argument strcat except that at most
Limit) Limit characters are appended.
strlen(Src_String) Returns an integer equal to the length of Src_String. (The
null character, ‘\0’, is not counted in the length.
strcmp(String_1, String_2) Returns 0 if String_1 and String_2 are the same. Returns a
value < 0 if String_1 is less than String_2. Returns a value > 0
if String_1 is greater than String_2 (that is, returns a
nonzero value if String_1 and String_2 are different). The
order is lexicographic.
strcmp(String_1, String_2, Limit) The same as the two-argument strcmp except that at most
Limit characters are compared.

CCS007L-Computer Programming 2 Page 2 of 8


Table 2 : Some Predefined character manipulating functions in <cctype>
FUNCTION DESCRIPTION
toupper(Char_Exp) Returns the uppercase version of Char_Exp (as value of type int).
tolower(Char_Exp) Returns the lowercase version of Char_Exp (as value of type int).
isupper(Char_Exp) Returns true provided Char_Exp is an uppercase letter; otherwise, returns false.
islower(Char_Exp) Returns true provided Char_Exp is an lowercase letter; otherwise, returns false.
isalpha(Char_Exp) Returns true provided Char_Exp is a letter of the alphabet; otherwise return false.
isdigit(Char_Exp) Returns true provided Char_Exp is one of the digits ‘0’ through ‘9’; otherwise,
returns false.
isalnum(Char_Exp) Returns true provided Char_Exp is either a letter or a digit; otherwise, returns false.
isspace(Char_Exp) Returns true provided Char_Exp is a whitespace character, such as the blank or
newline character, otherwise, return false.
ispunct(Char_Exp) Returns true provided Char_Exp is a printing character other than whitespace, a
digit, or a letter; otherwise return false.
isprint(Char_Exp) Returns true provided Char_Exp is a printing characters includes blank space;
otherwise returns false.
isgraph(Char_Exp) Returns true provided Char_Exp is a printing characters; otherwise returns false.
isctrl(Char_Exp) Returns true provided Char_Exp is a control character; otherwise, return false.

CCS007L-Computer Programming 2 Page 3 of 8


VI. GRADING SYSTEM/ RUBRIC

Trait (Excellent) (Good) (Fair) (Poor)


Able to identify Able to identify Able to identify only Unable to
correctly all input correctly all input one input or output identify any input
and output and and output (22-14pts) and output
Requirement
provide alternative. (25-17pts) (20-11pts)
Specification(30pts)
(28-20pts)

Able to apply Able to apply Able to identify Unable to


required data type required data type required data type or identify required
or data structure or data structure data structure but data type
Data type(20pts)
and produce and produce does apply correctly (9-11pts)
correct results (18- partially correct (12-14pts)
20pts) results (15-17pts)
The program works The program works The program The program
and meets all and meets all produces correct produce s
specifications. Does specifications. results but does not incorrect results
Input
exception al Does some display correctly Does (9-11pts)
Validation(20pts)
checking for errors checking for errors not check for errors
and out-of- range and out of range and out of range data
data (18-20pts) data (15-17pts) (12-14pts)
Unable to run Able to run Able to run program Able to run
program (10pts) program but have correctly without any program
Free from syntax, logic error (8-9pts) logic error and correctly without
logic, and runtime display inappropriate any logic error
errors (10pts) output (6-7pts) and display
appropriate
output (5pts)
The program was The program was The program was The program was
delivered on time delivered after 5 delivered after 10 delivered after
(10pts) minutes from the minutes from the 15 (or more)
Delivery (10pts)
time required. (8- time required. (6- minutes from the
9pts) 7pts) time required.
(5pts)
Use of Comments Specific purpose is Specific purpose is Purpose is noted for No comments
(10pts) noted for each noted for each each function. (6- included. (5pts)
function, control function and 7pts)
structure, input control structure.
requirements, and (8-9pts)
output results.
(10pts)

CCS007L-Computer Programming 2 Page 4 of 8


VII. LABORATORY ACTIVITY

INSTRUCTIONS:
Copy your source codes to be pasted in this document as well as a screen shot of your running
output.

ACTIVITY3.1: Compare two strings


Create a program that will compare two input strings using strcmp( ).

EXAMPLE PROGRAM OUTPUT:

CCS007L-Computer Programming 2 Page 5 of 8


#include<iostream>
using namespace std;
int main()
{
cout<<"**********************************"<<endl;
cout<<"S T R I N G C O M P A R E "<<endl;
cout<<"**********************************"<<endl;
char str1[50], str2[50];
int i=0, chk=0;
cout<<"\nEnter the First Word (str1): ";
cin>>str1;
cout<<"\nEnter the Second word (str2): ";
cin>>str2;
while(str1[i]!='\0' || str2[i]!='\0')
{
if(str1[i]!=str2[i])
{
chk = 1;
CCS007L-Computer Programming 2 Page 6 of 8
break;
}
i++;
}

if(chk==0)
cout<<"\nEqual";
else if (chk<1)
cout<<"\nNegative";
else
cout<<"\nPositive";
cout<<endl;
return 0;
}

ACTIVITY 3.2: Copying strings


Create a program that will copy a string from one variable to another using the strcpy( )
function.

EXAMPLE PROGRAM OUTPUT:

CCS007L-Computer Programming 2 Page 7 of 8


#include <cstring>

using namespace std;

int main()
{
char s1[100], s2[100];
cout<<"**********************************"<<endl;
cout<<"S T R I N G C O P Y "<<endl;
cout<<"**********************************"<<endl;
cout << "Enter The First Word (str1): ";
cin.getline(s1, 100);
cout << "Enter the Second Word (str2): ";
cin.getline(s2, 100);
strcpy(s1, s2);

cout << "new string value for str1: "<< s2 << endl;
return 0;
}

ACTIVITY 3.3: Concatenating strings


Create a program that will concatenate two strings.

CCS007L-Computer Programming 2 Page 8 of 8


CCS007L-Computer Programming 2 Page 9 of 8
#include <iostream>
using namespace std;

int main()
{
cout<<"**********************************"<<endl;
cout<<"S T R I N G C O N C A T E N A T E "<<endl;
cout<<"**********************************"<<endl;
string s1, s2, result;

cout << "Enter a First word: ";


getline (cin, s1);

cout << "Enter a Second word: ";


getline (cin, s2);

result = s1 + s2;

cout << "new string value for str1: "<< result;

return 0;
}

ACTIVITY 3.4: Palindrome


Convert a program that will determine if the given word input is a palindrome using C-
String functions.

NOTE: Palindromes are words that are read the same way either left to right or right to left.

EXAMPLE PROGRAM OUTPUT:

CCS007L-Computer Programming 2 Page 10 of


CCS007L-Computer Programming 2 Page 11 of
#include <iostream>
using namespace std;
int main()
{
char inputString[100];
int l, r, length = 0;
cout<<"**********************************"<<endl;
cout<<"P A L I N D R O M E "<<endl;
cout<<"**********************************"<<endl;
cout << "Enter a word: ";
cin >> inputString;

while(inputString[length] != '\0')
length++;

l = 0;
r = length -1;

while(l < r){


if(inputString[l] != inputString[r]){
cout<< inputString <<" is not a Palindrome"<< endl;
return 0;
}
l++;
r--;
}
cout<< inputString << " is Palindrome\n" << endl;

return 0;
}

ACTIVITY 3.5: Uppercase

CCS007L-Computer Programming 2 Page 12 of


Create a program that will accept an input string. Display the same string in all capital
form.

EXAMPLE PROGRAM OUTPUT:

CCS007L-Computer Programming 2 Page 13 of


#include <iostream>
using namespace std;

int main()
{
string str;
cout<<"Enter Some string: \n";
getline(cin,str);
for(int i=0;str[i]!='\0';i++)
{
if (str[i]>=65 && str[i]<=90 )
str[i] = str[i] + 32;
else if (str[i]>=97 && str[i]<=122 )
str[i] = str[i] - 32;
}

cout<< str;
return 0;

ACTIVITY 3.6: Strings to words


Create a program that will ask the user to enter some string. The program will split the
string in to word and display in reverse vertical order.

EXAMPLE PROGRAM OUTPUT:

CCS007L-Computer Programming 2 Page 14 of


#include <iostream>
#include <string>

using namespace std;

string reverse_words(string s)
{
int left = 0, i = 0, n = s.size();

while (s[i] == ' ')


i++;

left = i;

while (i < n)
{
if (i + 1 == n || s[i] == ' ')
{
int j = i - 1;
if (i + 1 == n)
j++;

while (left < j)


swap(s[left++], s[j--]);

left = i + 1;
}
if (s[left] == ' ' && i > left)
left = i;

i++;
}
reverse(s.begin(), s.end());
return s;
}

int main()
{

string str;
cout<<"Enter Some string: \n";
getline(cin,str);

str = reverse_words(str);

CCS007L-Computer Programming 2 Page 15 of


cout << str;

return 0;
}

VIII. QUESTION AND ANSWER

Briefly answer the questions below. Avoid erasures. For group activity, specify the name of
GROUP MEMBER/s who answered the question. Do not forget to include the source for all
NON-ORIGINAL IDEAS.

 What is a String?
Strings are like sentences. They're made up of a list of characters, which is actually an
"array of characters." Strings are extremely beneficial for transmitting information from
the program to the program's user. They are less useful when it comes to storing data
for the computer to use.
 What is cctype?
This header specifies a suite of functions for character classification and transformation.

 What is cstring?
CStrings are identical to regular strings, except they have an extra null character at the
end that distinguishes them as Cstrings. This null character represents the string's end.

CCS007L-Computer Programming 2 Page 16 of


IX. REFERENCES
 Zak, Dianne (2016). An Introduction to Programming with C++
 Deitel, Paul & Deitel, Harvey (2012). C++ How To Program, Eighth Edition
https://www.cprogramming.com/tutorial/lesson9.html

CCS007L-Computer Programming 2 Page 17 of

You might also like