0% found this document useful (0 votes)
4 views5 pages

DSA5_Activity_C_String (1)

The document outlines an activity for a Data Structures and Algorithms course at De La Salle University, focusing on the implementation of strings in C++. It includes various syntax examples for string manipulation, programming tasks, and instructions for students to analyze and solve problems using C++. Students are required to submit their program outputs and C++ code as part of the activity.

Uploaded by

Leeam Ramos
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
0% found this document useful (0 votes)
4 views5 pages

DSA5_Activity_C_String (1)

The document outlines an activity for a Data Structures and Algorithms course at De La Salle University, focusing on the implementation of strings in C++. It includes various syntax examples for string manipulation, programming tasks, and instructions for students to analyze and solve problems using C++. Students are required to submit their program outputs and C++ code as part of the activity.

Uploaded by

Leeam Ramos
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
You are on page 1/ 5

De La Salle University - Dasmariñas 1

CEAT – Engineering Department

T-CPET211LA – Data Structures and Algorithms


ACTIVITY 5: Strings
Objective: Be familiar with C++ implementation of Strings as a Data Structure and implementation using
functions
Instructions: Execute the following syntax to perform string methods, and complete the table by providing the
correct answers.

Syntax A #include<iostream>
#include<iostream> #include<string>
#include<string> using namespace std;
using namespace std;
void TestA(string);
void sample(string); void TestB(string);
int main() int main()
{ {
string first; string myString;
cout<<"Enter some string: "; cout<<"Enter some string: ";
getline(cin,first); getline(cin,myString);
for (int X = 0; X<first.size(); X++) cout<<myString<<endl;
{ TestA(myString);
cout<<first[X];
continuation
}
cout<<endl; TestB(myString);
for (int X = 0; X<first.size(); X++) return 0;
{ }
cout<<first.at(X);
} void TestA(string A){
return 0; A.erase(2,5);
} cout<<A<<endl;
}

Syntax B void TestB(string A){


#include<iostream> string X = "XXX";
#include<string> A.replace(2,5,X);
using namespace std; cout<<A<<endl;
}
int main()
{
string myString;
cout<<"Enter some string: "; Syntax D
getline(cin,myString); #include<iostream>
cout<<myString<<endl; #include<string>
myString.push_back('A'); using namespace std;
myString.push_back('B');
int main()
cout<<myString<<endl;
{
return 0;
string first,second;
}
cout<<"Enter first string: ";
getline(cin,first);
Syntax C cout<<"Enter second string: ";
getline(cin,second);
cout<<"First String: "<<first<<endl;
cout<<"Second String: "<<second<<endl;
first.swap(second);
cout<<"First String: "<<first<<endl;
cout<<"Second String: "<<second<<endl;
return 0;
}

T-CPET211 - Data Structures and Algorithms


De La Salle University - Dasmariñas 2
CEAT – Engineering Department

#include<iostream>
#include<string>
using namespace std;

void testSearch(string,string);
int main()
{
string haystack = "I am who I am";
string needle = "who";
testSearch(haystack,needle);
needle = "WHO";
testSearch(haystack,needle);
return 0;
}

Syntax E void testSearch(string A, string B){


#include<iostream> size_t posB = A.find(B);
#include<string>
using namespace std; if (posB==A.npos){
cout<<"NONE";
int main() }
{
string mainString = "Choco Pudding";
continuation
string sub; else{
sub = mainString.substr(5,4); cout<<"PRESENT";
cout<<sub<<endl; }
sub = mainString.substr(10,3); cout<<endl;
cout<<sub; }
return 0;
} Syntax H
#include<iostream>
Syntax F #include<string>
#include<iostream> using namespace std;
#include<string>
using namespace std; int main()
{
int main() string A = "I am who I am";
{ string B = "ZXI";
string haystack = "I am who I am"; size_t POS;
string needle = "am"; POS = A.find_first_of(B);
size_t a = haystack.find(needle); cout<<POS<<endl;
size_t b = haystack.rfind(needle); POS = A.find_last_of(B);
cout<<a<<endl<<b; cout<<POS;
return 0; return 0;
} }

*size_t is an unsigned integral type (the same as Syntax I


member type string::size_type). It returns the #include<iostream>
position of the first character of the first match. If no #include<string>
matches were found, the function returns using namespace std;
string::npos.
int main()
{
Syntax G char A = 'j';
char B = 'J';
cout<<"ASCII of"<<A<<": "<<int(A);
cout<<endl;
cout<<"ASCII of "<<B<<": "<<int(B);
return 0;
}

T-CPET211 - Data Structures and Algorithms


De La Salle University - Dasmariñas 3
CEAT – Engineering Department

ASCII stands for American Standard Code for


Information Interchange. Computers can only
understand numbers, so an ASCII code is the
numerical representation of a character or an action
of some sort. (See reference at the back of the
activity).

Syntax K
#include<iostream>
#include<string>
using namespace std;

int main()
{
string first, second;
cout<<"Enter first string: ";
getline(cin,first);
cout<<"Enter second string: ";
getline(cin,second);
if(first.compare(second)==0){
cout<<"Identical Strings";
Syntax J }
#include<iostream> else{
#include<string> cout<<"Not Identical";
using namespace std; }
return 0;
int main() }
{
string A = "j";
string B = "J";
cout<<A.compare(B)<<endl;
cout<<B.compare(A)<<endl;
cout<<B.compare("J")<<endl;
return 0;
}

T-CPET211 - Data Structures and Algorithms


De La Salle University - Dasmariñas 4
CEAT – Engineering Department

T-CPET211LA – Data Structures and Algorithms


ACTIVITY 5: Strings
Name: _______________________________________ Date of Submission___________________________

Instructions: Provide the answer for the following tables. Provide solution for the programming problems below.

Syntax where str = string DESCRIPTION

str.size()

str.length()

str.empty()

str.clear()

str[index]

str.at(index)

str.append(string)

str.assign(string)

str.assign(str,int,int)

str.push_back(char)

str.insert(int,string)

T-CPET211 - Data Structures and Algorithms


De La Salle University - Dasmariñas 5
CEAT – Engineering Department

str.erase(int,int)

str.replace(int,int,string)

str.swap(string)

str.substr(int,int)

str.find(string)

str.rfind(string)

str.find_first_of(string)

str.find_last_of(string)

str.compare(string)

stoi(string)

to_string(int)

Programming:
 Provide your analysis and solution of the following problems by writing and executing a C++ syntax.
 Upload the screenshot of your program output in a legal-size document.
 Upload your C++ codes in text editor file (ex. Notepad) with Courier New Size 11 single space
font

1. Input two strings and display the following:


a. The longest string between the two
b. The string with the most number of spaces
2. Input a sentence and display the following
a. The first word and last word of the sentence
b. Number of words in the sentence
c. Two substrings, the first half of and the second half of the size of the sentence.
3. Create a program that will arrange a string alphabetically

T-CPET211 - Data Structures and Algorithms

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy