2 Lecture 2 - Strings and Functions
2 Lecture 2 - Strings and Functions
2 Lecture 2 - Strings and Functions
MANIPULATION
ITPROGX2 – Computer
Programming 2 with Data
Structures for Games
C-STRING
A string stored as an array of characters terminated with ‘\0’
An Array type for Strings
greet[6]=“Hello” (where Hello is a literal string)
greet[0] = ‘H’
greet[1] = ‘e’
greet[2] = ‘l’
greet[3] = ‘l’
greet[4] = ‘l’
greet[5] = ‘\0’ – end marker, null character (sentinel value)
C-STRING
legal
strcpy(aString,"Hello");
C-STRING
illegal
char aString[10] = "Hello";
if(aString == "Hello")
cout << "Hello";
legal
char aString[10] = "Hello";
if(strcmp(aString,"Hello")==0)
cout << "Hello";
TABLE: 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
strncpy(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
strncat(Target_String_Var,Src_String, Limit) The same as the two argument strcat except that at most 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.
strncmp(String_1, String_2, Limit) The same as the two-argument strcmp except that at most Limit
characters are compared.
TABLE: SOME PREDEFINED
C-STRING FUNCTIONS IN <cstring>
#include <iostream>
#include<cstring>
using namespace std;
int main()
{ char x[]="good morning ";
char y[25], z[15];
cout<<"The string character in array x is " << x<<endl;
cout<<"The string character in array y is " << strcpy(y,x)<<endl;
strncpy(z,y,4);
z[5]='\0';
cout<<"The string character in array z is " << z<<endl;
system("pause>0");
return 0; }
TABLE: SOME PREDEFINED
C-STRING FUNCTIONS IN <cstring>
Source Code:
OUTPUT:
TABLE: SOME PREDEFINED
C-STRING FUNCTIONS IN <cstring>
FUNCTION DESCRIPTION
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.
OUTPUT:
The member function getline can be used to read a line of
getline input and place the string of characters on that line into a C-
string variable.
syntax:
cin.getline(String_Var, Max_Characters + 1);
Source Code:
OUTPUT:
CHARACTER MANIPULATION TOOLS
get function allows your program to read in one character of
input and store it in a variable of type char
SOURCE CODE:
char a,b,c;
cout<<"Enter any 3 characters ";
cin.get(a); OUTPUT:
cin.get(b);
cin.get(c);
cout<<c<<b<<a;
CHARACTER MANIPULATION TOOLS
SOURCE CODE:
CHARACTER MANIPULATION TOOLS
put This function member is analogous to the member function get
except that it is used for output rather than input. The function put
allows your program to output one character
SOURCE CODE:
OUTPUT:
CHARACTER-MANIPULATING FUNCTIONS
Table: Some functions in <cctype>
FUNCTION s
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 returns 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, returns false.
ispunct(Char_Exp) Returns true provided Char_Exp is a printing character other than whitespace, a digit, or a
letter; otherwise returns 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, returns false.
CHARACTER-MANIPULATING FUNCTIONS
toupper(Char_Exp)
Returns the uppercase version of Char_Exp (as
value of type int).
CHARACTER-MANIPULATING FUNCTIONS
tolower(Char_Exp)
Returns the lowercase version of
Char_Exp (as value of type int).
CHARACTER-MANIPULATING FUNCTIONS
isupper(Char_Exp)
Returns true provided Char_Exp is
an uppercase letter; otherwise,
returns false.
CHARACTER-MANIPULATING FUNCTIONS
islower(Char_Exp)
Returns true provided Char_Exp is
an lowercase letter; otherwise,
returns false.
CHARACTER-MANIPULATING FUNCTIONS
isalpha(Char_Exp)
Returns true provided Char_Exp is
a letter of the alphabet; otherwise
returns false.
CHARACTER-MANIPULATING FUNCTIONS
isdigit(Char_Exp)
Returns true provided Char_Exp is
one of the digits ‘0’ through ‘9’;
otherwise, returns false.
CHARACTER-MANIPULATING FUNCTIONS
isalnum(Char_Exp)
Returns true provided Char_Exp is
either a letter or a digit; otherwise,
returns false.
CHARACTER-MANIPULATING FUNCTIONS
isspace(Char_Exp)
Returns true provided Char_Exp is
a whitespace character, such as
the blank or newline character,
otherwise, returns false.
CHARACTER-MANIPULATING FUNCTIONS
isprint(Char_Exp)
Returns true provided Char_Exp is
a printing characters includes blank
space; otherwise returns false.
CHARACTER-MANIPULATING FUNCTIONS
isgraph(Char_Exp)
Returns true provided Char_Exp is
a printing characters; otherwise
returns false.
CHARACTER-MANIPULATING FUNCTIONS
ispunct(Char_Exp)
Returns true provided Char_Exp is
a printing character other than
whitespace, a digit, or a letter;
otherwise returns false.
CHARACTER-MANIPULATING FUNCTIONS
Table: Member Functions of the Standard Class String
Example Remarks
Constructor
string str; Default constructor; creates empty string object str.
string str(“string”); Creates string object with data “string”.
string str(aString); Creates a string object str that is a copy of aString. aString is an object
of the class string.
Element Access
str[i] Returns read/write reference to character in str at index i.
str.at(i) Returns read/write reference to character in str at index i.
str.substr(position, length) Returns the substring of the calling object starting at position and
having length characters.
CHARACTER-MANIPULATING FUNCTIONS
Table: Member Functions of the Standard Class String
Example Remarks
Assignment/Modifiers
str1 = str2 Allocates space and initializes it to Str2’s data, releases memory
allocated for str1, and set str1’s size to that of str2.
str1 += str2 Character data of str2 is concatenated to the end of str1; the size is set
appropriately.
str.empty( ) Returns true if str is an empty string; returns false otherwise.
str1 + str2 Returns a string that has str2’s data concatenated to the end of str1’s
data. The size is set appropriately.
str.insert(pos, str2) Insert str2 into str beginning at position pos.
str.remove(pos, length) Removes substring of size length, starting at position pos.
CHARACTER-MANIPULATING FUNCTIONS
Table: Member Functions of the Standard Class String
Example Remarks
Comparisons
str1 == str2 Compare for equality or inequality; returns a Boolean value.
str1 != str2
str1 < str2 str1 > str2 Four comparisons. All are lexicographical comparisons
str1 <= str2 str1 >= str2
str.find(str1) Returns index of the first occurrence of str1 in str.
str.find(str1, pos) Returns index of the first occurrence of string str1 in str; the search
starts at position pos.
str.find_first_of(str1, pos) Returns the index of the first instance in str of any character in str1,
starting the search at position pos.
str.find_first_not_of(str1,pos) Returns the index of the first instance in str of any character not in str1,
starting search at position pos.
SOURCE CODE
SOURCE CODE
SOURCE CODE
SOURCE CODE