Problems
Many students have difficulty handling strings.
Therefore, today we will introduce an easier and simpler way to work with strings.
C++
The C++ string class is a class for handling strings.
Unlike C's char* or char[] strings, C++ strings do not have a terminating '\0' character and their length can change dynamically.
However, std::string cannot be directly input using scanf.
So, you should use C++ standard input/output cin and cout.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string word, sentence; // declare two strings
cin >> word; // input until a space (or enter)
cin.ignore(); // remove one character from input buffer (needed before getline to remove '\n', don't use if getline is used alone)
getline(cin, sentence); // input the rest of the line including spaces until '\n'
cout << word << '\n'; // output
cout << sentence << '\n'; // output
return 0;
}
Using the string class is convenient because operators can be used.
You can use ==, <, >, +, etc.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a = "abc", b = "de", c;
// If a equals b, c will be the same as a.
// If a is lexicographically smaller than b, c will be a concatenated with b.
// Otherwise, c will be b concatenated with a.
if(a == b) c = a; // c = "abc"
else if(a < b) c = a + b; // c = "abcde"
else c = b + a; // c = "deabc"
cout << c;
return 0;
}
You can also easily manipulate strings using various functions.
Let’s learn these functions using the following example:
Get string length:
length()Find the index of another string:
find(other_string)Extract a substring:
substr(start_index, length)Erase a part of the string:
erase(start_index, length)
#include <iostream>
#include <string>
using namespace std;
int main()
{
string A = "I love coding!";
// Get length of A
int len = (int) A.length(); // len: 14
// Find index of substring "coding"
int idx = A.find("coding"); // idx: 7
// Find index of substring "abc"
int x = A.find("abc"); // if not found, returns string::npos
// Extract substring of length 4 from index 2: "love"
string sub = A.substr(2, 4);
// Delete substring of length 5 starting from index 1
A.erase(1, 5); // A: "I coding!"
cout << len << "\n" << idx << "\n" << x << "\n" << sub << "\n" << A;
return 0;
}
#include <stdio.h>
#include <string>
using namespace std;
int main()
{
char _word[105], _sent[105];
scanf("%s %[^\r\n]", _word, _sent);
string word = _word, sent = _sent;
word += '!';
sent += '?';
strcpy(_word, word.c_str());
strcpy(_sent, sent.c_str());
printf("%s\n%s", _word, _sent);
return 0;
}Python
Get string length:
len()Find index of substring:
find(other_string)Extract substring: slicing
[start_index : end_index](end_index not included)Remove part of a string:
replace(old, new[, count])
A = "I love coding!"
# Get length
length = len(A) # length: 14
# Find index of "coding"
idx = A.find("coding") # idx: 7
# Find index of "abc"
x = A.find("abc") # if not found, returns -1
# Extract substring from index 2 to 5: "love"
sub = A[2:6]
# Remove "co" from string
A = A.replace('co', '') # A: "I love ding!"
print(length, idx, sub)
print(A)
[Problem]
Write a program that satisfies the following conditions:
Input two strings: S and T.
Starting from the first character of S, check for T. When T appears, remove it and concatenate the remaining parts.
Repeat step 2 until T no longer appears in S.
Output the remaining string S.
Input
First line: string S (1 ≤ length of S ≤ 100)
Second line: string T (1 ≤ length of T ≤ length of S)
All strings contain no spaces.
Output
Output the remaining string S after processing.
It is guaranteed that some characters remain in S.
Example
whatthemomooofun
moo
whatthefun