Tuesday, November 9, 2010

[DSA] string class

I hope you have enjoyed and learned some "know how" of writing a program from scratch, with our lil' palindrome example.

To make our lives easier, C++ have lots of library for us to choose from
#include < string > is one of them. Take note string and cstring. both are different!

If you check out the book pg260. There is a piece of code that is very similar of what we have done today. It is using CString instead of String.

Drill Question

Write a name generator (star wars name anybody? hehe) software.
1. The software takes in a name for input and
2. think of the "magic" algorithm to generate a "NINJA" name. Or any name modifiers you can think of. Similar to those name generators you have played on facebook. I guess pretty much of everybody have generated at least once.  Code it FREE STYLE ~maybe you want to start by storing 10 sample first name and 10 sample last name. Which gives a 10 to 10 mapping. Easily 100 names~

Append part of your original name to the sample name "randomly".
3. Check out the string member functions attached~

below are the member functions of String. http://www.cplusplus.com/reference/string/string/

Member functions

(constructor) Construct string object (constructor member)
operator= String assignment (public member function)

begin Return iterator to beginning (public member function)
end Return iterator to end (public member function)
rbegin Return reverse iterator to reverse beginning (public member function)
rend Return reverse iterator to reverse end (public member function)

size Return length of string (public member function)
length Return length of string (public member function)
max_size Return maximum size of string (public member function)
resize Resize string (public member function)
capacity Return size of allocated storage (public member function)
reserve Request a change in capacity (public member function)
clear Clear string (public member function)
empty Test if string is empty (public member function)

Element access:
operator[] Get character in string (public member function)
at Get character in string (public member function)

operator+= Append to string (public member function)
append Append to string (public member function)
push_back Append character to string (public member function)
assign Assign content to string (public member function)
insert Insert into string (public member function)
erase Erase characters from string (public member function)
replace Replace part of string (public member function)
copy Copy sequence of characters from string (public member function)
swap Swap contents with another string (public member function)

String operations:
c_str Get C string equivalent (public member function)
data Get string data (public member function)
get_allocator Get allocator (public member function)
find Find content in string (public member function)
rfind Find last occurrence of content in string (public member function)
find_first_of Find character in string (public member function)
find_last_of Find character in string from the end (public member function)
find_first_not_of Find absence of character in string
find_last_not_of Find absence of character in string from the end (public member function)
substr Generate substring (public member function)
compare Compare strings (public member function)


Sebastian said...


Sebastian said...


alvinthen dcpe/ft/2b/03 said...


Felix (DCPE 2b03) said...


yao de DCPE 2B03 said...


heng gnee dcpe/ft/2b/03 said...


Qi Hao(DCPE/2B/03) said...


heng gnee dcpe/ft/2b/03 said...


keekuan (DCPE2B03) said...


Li Xiaofei DCPE 2B03 said...


XuZhou said...



Tin Tun(dcpe/2b/03) said...


Sathis 2b03 said...


Yi Wei said...


Sathis 2b03 said...


alvinthen dcpe/ft/2b/03 said...


heng gnee dcpe/ft/2b/03 said...


Felix (2b03) said...


Qi Hao(DCPE/2B/03) said...


sjteo said...

@guys, nice work. keep the codes coming in~!~ RAWWRRRR

Wang Liang said...