Sie sind auf Seite 1von 14

ECC3101 COMPUTER

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

To understand the concept of string and


array
To declare, initialize, assign and access
string
To implement C-string and character
functions

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

Implemented either using


Pointer syntax
Array syntax

DECLARATION

Technology
Oriented

Environmental
Sustainable Development
Business Driven
Friendly

char city[7] = Dallas; // array


//syntax
char *pCity = Dallas; // pointer
//syntax

Each creates sequence of


characters D, a, l, l, a, s,
\0

ACCESSING
STRING

cout
cout
cout
cout

<<
<<
<<
<<

Technology
Oriented

Environmental
Sustainable Development
Business Driven
Friendly

city[1] << endl; //array


*(city + 1) << endl; //pointer
pCity[1] << endl;
*(pCity + 1) << endl;

Output:
a
Note:
city cant be reassigned after declaration but
pCity can.

READING
STRING

Technology
Oriented

Environmental
Sustainable Development
Business Driven
Friendly

String can be read from the


keyboard using
1. cin OR
2. cin.getline(char array[], int
size, char delimitChar)
E.g. 1:
char city[30];
cout << "Enter a city: ";
cin >> city;
cout << "You entered " << city << endl;

Technology
Oriented

Environmental
Sustainable Development
Business Driven
Friendly

E.g. 2: cin.getline(char array[], int


size, char delimitChar)
char city[30];
cout << "Enter a city: ";
cin.getline(city, 30, '\n');
cout << "You entered " << city << endl;
Note:
delimitChar is \n by default

C-STRING
FUNCTIONS

Technology
Oriented

Environmental
Sustainable Development
Business Driven
Friendly

Function

Description

int strlen(char *s1)

Returns string length

char strcpy(char *s1, const


char *s2)

Copies s2 to s1. s1 value is returned

char strncpy(char *s1, const


char *s2, size_t n)

Copies at most n characters in s2 to


s1. s1 value is returned

char strcat(char *s1, const


char *s2)

Append s2 to s1. First character of


s2 overwrites null of s1. s1 value is
returned

char strncat(char *s1, const


char *s2, size_t n)

Append at most n characters of s2


to s1. First character of s2
overwrites null of s1. s1 value is
returned

char strcmp(char *s1, const


char *s2)

Returns value less than 0, 0 or


greater than 0, if s1 > s2, s1 == s2
or s1 < s2, respectively

char strncmp(char *s1, const


char *s2, size_t n)

Returns value less than 0, 0 or


greater than 0, if s1 > s2, s1 == s2
or s1 < s2 (for the first n
characters), respectively

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

<<
<<
<<
<<

strcmp(s1, s2) << endl;


strcmp(s2, s1) << endl;
strcmp(s2, s3) << endl;
strncmp(s1, s2, 3) << endl;

return 0;
}

CHARACTER
FUNCTIONS

Technology
Oriented

Environmental
Sustainable Development
Business Driven
Friendly

Function

Description

isdigit(a)

Returns true if a is a digit

isalpha(a)

Returns true if a is a letter

isalnum(a)

Returns true if a is a letter or a digit

islower(a)

Returns true if a is a lowercase letter

isupper(a)

Returns true if a is an uppercase letter

tolower(a)

Returns the lowercase equivalent of a, if a is an


uppercase letter, otherwise returns itself

toupper(a)

Returns the uppercase equivalent of a, if a is a


lowercase letter, otherwise returns itself

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;
}

Das könnte Ihnen auch gefallen