Download presentation
Presentation is loading. Please wait.
1
String in C++
2
Using Strings in C++ Programs
String library <string> or <cstring> provides functions to: - manipulate strings - compare strings - search strings ASCII character code - Strings are compared using their character codes - Easy to make comparisons (greater than, less than, equal to)
3
Using Strings in C++ Programs .. Cont.
Character Constant - Integer value of a character - Represented with single quotes - ‘z’ is the integer value of z, which is 122 String in C++ - Series of characters treated as one unit - can include letters, digits, special characters +, -, *, … - String literal (string constants) enclosed in double quotes, for example: “C++ course”
4
Using Strings in C++ Programs .. Cont.
Example: Write a C++ program that reads two initials and the last name of a person and displays a personalized message to the program user. Analysis stage: Input: 2 characters for the initials (e.g. first and second) 1 string for the last name (e.g. last) Output: a message to welcome the user
5
Using Strings in C++ Programs .. Cont.
//A program to display a user’s name with a welcome message #include <iostream> #include <string> using namespace std; void main ( ) { char first, second; //input and output: first and second initials string last; //input and output: last name // Enter letters and print message. cout<<"Enter 2 initials for your first and second names and last name: " ; cin >> first >> second >> last; cout<< "Hello "<<first<< ". " << second<<". " <<last<< endl; }
6
Using build in library. #include <iostream> #include <string> using namespace std; void main () {string str1 = "Hello"; string str2 = "World"; string str3; int len ; str3 = str1; // copy str1 into str3 cout << "str3 : " << str3 << endl; // concatenates str1 and str2 str3 = str1 + str2; len = str3.size(); cout << "str3.size() : " << len <<endl; }
7
Using built-in libraray – Cont.
#include<iostream> #include<string> using namespace std; void main() { string first, last, title; string wholeName; string greeting= "Hello "; cout<<"Enter first name: "; cin>>first; cout<<"Enter last name: "; cin>>last; wholeName=first+" "+last; cout<<greeting<<wholeName<<'!'<<endl; cout<<"You have "<<(wholeName.length()-1) <<" letters in your name. "<<endl; cout<<"Your initials are "<<first.at(0) <<last.at(0)<<endl; cout<<"position of last "<<wholeName.find(last)<<endl; wholeName.insert(0,"Mr. "); cout<<greeting<<wholeName<<'!'<<endl; wholeName.insert(9,"Ali "); wholeName.replace(9,7,"Maher "); wholeName.erase(9,3); title.assign(wholeName,0,20); cout<<greeting<<title<<'!'<<endl; }
8
Output :
9
Fundamentals of Strings in C++
String can be array of characters ends with null character ‘\0’. char color [ ] = “green” ; -- this creates 6 element char array, color, (last element is ‘\0’) -- color can be declared also as : char color [ ] = {‘g’, ‘r’, ‘e’, ‘e’, ‘n’, ‘\0’ }; char color [ 6] = {‘g’, ‘r’, ‘e’, ‘e’, ‘n’, ‘\0’ }; g r e n \0
10
Fundamentals of Strings in C++ .. Cont.
- String can be constant pointer that points to the string’s first character. Example: char *colorPtr = “green” ; -- this creates pointer variable colorPtr that points to the string “green” that is stored somewhere in memory -- value of variable colorPtr is the address of its first character(g) g r e n \0
11
Example void main( ) { char firstName[] = "Ahmad";
char *lastName = "Omar"; cout<<"First Name: "<<firstName<<endl; cout<<"Last Name: "<<lastName<<endl; int i=0; cout<<"FirstName: "; while (firstName[i] != '\0') cout<<firstName[i++]; i=0; cout<<"\nLast Name: "; while (lastName[i] != '\0') cout<<lastName[i++]; }
12
Fundamentals of Strings in C++ .. Cont.
Reading Strings - Assign input to character array, for example char word [ 20 ]; cin >> word; cout<<word<<endl; -- this reads characters until a space, tab, newline, or end-of-file is encountered. -- the string should be less than 19 characters, the 20th is for the null character (‘\0’). Problem: read characters until the first white space
13
Fundamentals of Strings in C++ .. Cont.
solution: To read an entire line of text into an array, C++ uses: getline function as follows: cin.getline ( array, array size, delimiter character); - getline will copy input into specified array until either -- one less than the size is reached -- the delimiter character is input - Example: char word [20] ; cin.getline ( word, 20, ‘\n’ );
14
String Manipulation Functions
Description Function Copies string s2 into the character array s1. The value of s1 is returned. char *strcpy(char *s1, const char *s2); Copies at most n characters of string s2 into the array s1. The value of s1 is returned. char *strncpy(char *s1, const char *s2, size_t n); Appends string s2 to string s1. The value of s1 is returned. char *strcat (char *s1, const char *s2); Appends at most n characters of string s2 to string s1. The value of s1 is returned. char *strncat (char *s1, const char *s2, size_t n);
15
String Manipulation Functions .. Cont.
Compares string s1 with string s2. The function returns a value of zero, less than zero or greater than zero if s1 is equal to, less than or greater than s2, respectively. int strcmp(const char *s1, const char *s2); Compares up to n characters of string s1 with string s2. The function returns zero, less than zero or greater than zero if s1 is equal to, less than or greater than s2, respectively. int strncmp(const char *s1, const char *s2, size_t n);
16
String Manipulation Functions .. Cont.
Determines the length of string s. The number of characters preceding the terminating null character is returned. Size_t strlen( const char *s); Note: These functions only work with Array of characters and pointer. They don’t work on variables of type string.
17
String Manipulation Functions .. Cont.
1- strcpy(s1, s2) s1 = s2 Copies string s2 into string s1. #include <iostream> #include <cstring> using namespace std; void main() { char str1[] ="Omar"; char *str2 = "Ahmad"; strcpy(str1,str2); cout<<str1<<endl; }
18
2- strncpy(s1, s2) s1[n] = s2[n]
#include <iostream> #include <cstring> using namespace std; void main() { char str1[] = ”**********"; char *str2 = “$$$$$$$$$$"; strncpy(str1,str2,5); cout<<str1<<endl; }
19
Concatenates string s2 onto the end of string s1.
3- strcat(s1, s2) s1 = s1+s2 Concatenates string s2 onto the end of string s1. #include <iostream> #include <cstring> using namespace std; void main() { char str1[24] = ”Philadelphia"; char *str2 = “University"; strcat(str1,str2); cout<<str1<<endl; }
20
4- strncat(s1, s2,n) s1 = s1+s2[n]
#include <iostream> #include <cstring> using namespace std; void main() { char str1[24] = ”Philadelphia"; char *str2 = “University of Jordan"; strncat(str1,str2,10); cout<<str1<<endl; }
21
5- strcmp(s1, s2) 0 if s1 = s2 -1 if s1 < s2 1 if s1 > s2
Symbols < … < numbers < … < capital letters < …. < smal letters. #include <iostream> #include <cstring> using namespace std; void main() { char str1[20] ; char str2[20] ; cin.getline(str1,20); cin.getline(str2,20); if (strcmp(str1,str2)) if (strcmp(str1,str2) == 1) cout<<str1<<" > "<<str2<<endl; else cout<<str1<<" < "<<str2<<endl; cout<<str1<<" = "<<str2<<endl; }
22
6- strncmp(s1, s2,n) 0 if s1[n] = s2[n]
#include <iostream> #include <cstring> using namespace std; void main() { char str1[20] ; char str2[20] ; cin.getline(str1,20); cin.getline(str2,20); if (strncmp(str1,str2,1)) if (strcmp(str1,str2,1) == 1) cout<<str1<<" > "<<str2<<endl; else cout<<str1<<" < "<<str2<<endl; cout<<str1<<" = "<<str2<<endl; }
23
7- strlen(s) How many characters in s
is a function that accepts a string, defined as an array of characters, and returns the number of characters in the string excluding null character #include <iostream> #include <cstring> using namespace std; void main() { char s1[] = "Ahamd Ali"; char *s2 = "Amman City"; cout<<s1<<" Consists of "<<strlen(s1)<<" Characters.\n"; cout<<s2<<" Consists of "<<strlen(s2)<<" Characters.\n"; }
24
strcpy - code #include<iostream> using namespace std;
const int stringsize = 40; void strcpy(char destation[], char source[]) { int stringindex=0; while(source[stringindex] !='\0') { destation[stringindex]=source[stringindex]; stringindex++;} destation[stringindex]='\0'; } void main() { char s1[stringsize]; // or you can define it char *s1 = new char[]; strcpy(s1,"we are going to implement strings"); cout<<"S1 = "<< s1<<endl;
25
strlen- code #include<iostream> using namespace std;
const int stringsize =256; int strlen(char string[]) { int len=0; while(string[len]!='\0') len++; return len;} void main() { char buffer[stringsize]; char buffer2[] = "this is another expression"; cout<<"enter an expression"<<endl; cin>>buffer; cout<<"the length of the string you just typed is " <<strlen(buffer)<<endl; cout<<"the length of the string with space is " <<strlen(buffer2)<<endl;}
26
strcmp - code #include<iostream> using namespace std;
const int stringsize = 256; int strcmp(char s1[], char s2[]) { int stringindex=0; while(s1[stringindex] !='\0') { if (s1[stringindex]>s2[stringindex]) return 1; else if (s1[stringindex]<s2[stringindex]) return -1; else stringindex++;} if (strlen(s1) < strlen(s2)) return 0;} void main() { char b1[]="Ahmad"; char b2[]="Ali"; if(strcmp(b1,b2) == -1) cout<<"first string is smaller"<<endl; else if (strcmp(b1,b2) == 1) cout<<"first string is larger"<<endl; else cout <<"both strings are the same"<<endl;}
27
strcat - code void main() { char s1[265];
#include<iostream> using namespace std; void strcat(char destination[], char source[]) { int len=0; int stringindex=0; // calculates the end of the destination string while(destination[len]!='\0') len++; while(source[stringindex] != '\0') { destination[len+stringindex]=source[stringindex]; stringindex++; } destination[len+stringindex]='\0'; void main() { char s1[265]; strcpy(s1,"philadelphia University "); strcat(s1,"- Faculty of Information Technology"); cout<<" the string is: "<<endl<<s1<<endl; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.