Beruflich Dokumente
Kultur Dokumente
PROGRAMMING
String
Dr. Siti Barirah Ahmad Anas
barirah@upm.edu.my
03-89466439
Room A.04.89
OUTLINE
Technology
Oriented
Environmental
Sustainable Development
Business Driven
Friendly
Introduction to String
Declaration
Accessing String
C-String Functions
Character Functions
LEARNING
OUTCOME
Technology
Oriented
Environmental
Sustainable Development
Business Driven
Friendly
INTRODUCTION
Technology
Oriented
Environmental
Sustainable Development
Business Driven
Friendly
A pointer-based string
Array of characters ending with a null
terminator \0
\0 indicates string termination in
memory
DECLARATION
Technology
Oriented
Environmental
Sustainable Development
Business Driven
Friendly
ACCESSING
STRING
cout
cout
cout
cout
<<
<<
<<
<<
Technology
Oriented
Environmental
Sustainable Development
Business Driven
Friendly
Output:
a
Note:
city cant be reassigned after declaration but
pCity can.
READING
STRING
Technology
Oriented
Environmental
Sustainable Development
Business Driven
Friendly
Technology
Oriented
Environmental
Sustainable Development
Business Driven
Friendly
C-STRING
FUNCTIONS
Technology
Oriented
Environmental
Sustainable Development
Business Driven
Friendly
Function
Description
EXAMPLE - STRCPY
Technology
Oriented
Environmental
Sustainable Development
Business Driven
Friendly
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char s1[20];
char s2[] = "Dallas, Texas";
char s3[] = "AAAAAAAA";
strcpy(s1, s2);
strncpy(s3, s2, 6);
s3[6] = '\0';
cout
cout
cout
cout
<<
<<
<<
<<
s1 << endl;
s2 << endl;
s3 << endl;
strlen(s3) << endl;
return 0;
}
EXAMPLE - STRCAT
Technology
Oriented
Environmental
Sustainable Development
Business Driven
Friendly
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char s1[20] = "Dallas";
char s2[] = "Texas, USA";
char s3[] = "Dallas";
strcat(strcat(s1, ", "), s2);
strncat(strcat(s3, ", "), s2, 5);
cout
cout
cout
cout
cout
<<
<<
<<
<<
<<
s1 << endl;
s2 << endl;
s3 << endl;
strlen(s1) << endl;
strlen(s3) << endl;
return 0;
}
EXAMPLE - STRCMP
Technology
Oriented
Environmental
Sustainable Development
Business Driven
Friendly
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char *s1 = "abcdefg";
char *s2 = "abcdg";
char *s3 = "abcdg";
cout
cout
cout
cout
<<
<<
<<
<<
return 0;
}
CHARACTER
FUNCTIONS
Technology
Oriented
Environmental
Sustainable Development
Business Driven
Friendly
Function
Description
isdigit(a)
isalpha(a)
isalnum(a)
islower(a)
isupper(a)
tolower(a)
toupper(a)
AMPLE CHARACTER
FUNCTIONS
#include <iostream>
using namespace std;
Technology
Oriented
Environmental
Sustainable Development
Business Driven
Friendly
int main()
{
char ch;
cout << "Enter a character: " << endl;
cin >> ch;
if(islower(ch))
{
cout << "\nIt is a lowercase letter";
cout << "\nIts equivalent uppercase is " << (char)toupper(ch) <<
endl;
}
else if(isupper(ch))
{
cout << "\nIt is an uppercase letter";
cout << "\nIts equivalent lowercase is " << (char)tolower(ch) <<
endl;
}
else if(isdigit(ch))
{
cout << "\nIt is a digit" << endl;
}