0% found this document useful (0 votes)
10 views

Lec 05 Strings

c++ string

Uploaded by

wwerazo8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lec 05 Strings

c++ string

Uploaded by

wwerazo8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Computer Language II

Lecture 5: Strings

Dr. Khari Armih

khari.armih@gmail.com

June 16, 2023

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 16, 2023 1 / 22


Outline
❶ What is string
❷ Type of string
❸ The C-Style String Library

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 16, 2023 2 / 22


String
• Series of characters treated as single unit
• Can include letters, digits, special characters +,-,* .....
• Enclosed in double quotes
– ”Computer language”
• Array of characters, ends with null character ’\0’
• String is constant pointer
– pointer to the first character

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 16, 2023 3 / 22


Type of String
• C++ provides following two types of string representations:
– The C-style character string
– The string class type introduced with Standard C++

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 16, 2023 4 / 22


The C-Style Character String
• String assignment

char str1[5] = {’H’,’e’,’l’,’l’,’o’};

or

char str2[] = "Hello";


• The C++ compiler automatically places the ’\0’ at the end of the
string when it initializes the array

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 16, 2023 5 / 22


Taking String Input
1 #include <iostream>
2 using namespace std;
3

4 int main()
5 {
6 char name[30];
7 cout << "What is your name: ";
8 cin >> name;
9 cout << "Hi, " << name;
10 return 0;
11 }

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 16, 2023 6 / 22


Sample run
What is your name: Khari
Hi, Khari

What is your name: Khari Armih


Hi, Khari

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 16, 2023 7 / 22


Using cin.get() Function
1 #include <iostream>
2 using namespace std;
3 int main()
4 {
5 char name[30];
6 cout << "What is your name: ";
7 cin.get(name,30);
8 cout << "Hi, " << name;
9 return 0;
10 }

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 16, 2023 8 / 22


Sample run
What is your name: Khari Amer Armih
Hi, Khari Amer Armih

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 16, 2023 9 / 22


Using loop to print a string
1 #include <iostream>
2 using namespace std;
3 int main()
4 {
5 char str[] ="Programming is fun!";
6 int i=0;
7 while(str[i]!=’\0’)
8 {
9 cout << str[i];
10 i++;
11 }
12 return 0;
13 }

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 16, 2023 10 / 22


The C-Style String Library
• The more commonly-used string functions:
– strcat - concatenate two strings
– strchr - string scanning operation
– strcmp - compare two strings
– strcpy - copy a string
– strlen - get string length
– strncat - concatenate one string with part of another
– strncmp - compare parts of two strings
– strncpy - copy part of a string
• You must include cstring library

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 16, 2023 11 / 22


Full Example
1 #include <iostream>
2 #include<cstring>
3 using namespace std;
4 int main()
5 {
6 char line[] ="I am studying computer language.";
7
8 cout << "line: " << line << endl;
9
10 cout <<"strlen(line): "<< strlen(line) << endl;
11
12 strcat(line," How about you?");
13
14 cout << "line: " << line << endl;
15
16 char newline[100];
17
18 strcpy(newline, line);
19
20 cout << "strcpy(newline, line): " << newline << endl;
21
22 return 0;
23 }
Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 16, 2023 12 / 22
Sample run
line: I am studying computer language.
strlen(line): 32
line: I am studying computer language. How about
you?
strcpy(newline, line): I am studying computer
language. How about you?

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 16, 2023 13 / 22


Passing string to function
1 #include <iostream>
2 #include<cstring>
3 using namespace std;
4
5 int len(char *str)
6 {
7 int c=0;
8 while(str[c] != ’\0’)
9 c++;
10 return c;
11 }
12
13 int main()
14 {
15 char line[] ="I am studying computer language.";
16
17 cout <<"strlen(line): "<< strlen(line) << endl;
18
19 cout << "len(line): " << len(line) << endl;
20
21 return 0;
22 }
Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 16, 2023 14 / 22
Sample run
strlen(line): 32
len(line): 32

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 16, 2023 15 / 22


In-Class Exercise
1 void mystrcpy(char *str1, char *str2)
2 {
3 int i=0;
4 while(str1[i] != ’\0’)
5 {
6 str2[i] = str1[i];
7 i++;
8 }
9 str2[i]= ’\0’;
10 }

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 16, 2023 16 / 22


The String Class in C++
• C++ has a ¡string¿ library
• Include it in your programs when you wish to use strings:
#include < string >
• In this library, a class string is defined and implemented
• It is very convenient and makes string processing easier than in C

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 16, 2023 17 / 22


Declaration of strings
• The following instructions are all equivalent
– string str = "Computer Language";
– string str = ("Computer Language");
• They declare str to be an object of type string, and assign the
string ”Computer Language”

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 16, 2023 18 / 22


Operation on strings
• Concatenation (+)

1 #include <iostream>
2 #include<string>
3 using namespace std;
4 int main()
5 {
6 string str1 ="I am studying computer language.";
7 string str2;
8 str2 = str1 + " What about you?";
9 cout << "str2: " << str2 << endl;
10 return 0;
11 }

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 16, 2023 19 / 22


Comparison Operators
• We can compare two strings x and y using the following
operators: ==, !=, <, <=, >, >=
• The comparison is alphabetical
• The outcome of each comparison is: true or false

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 16, 2023 20 / 22


In-Class Exercise
1 #include <iostream>
2 #include<string>
3 using namespace std;
4 int main()
5 {
6 string str1 ="I am studying computer language.";
7 string str2;
8

9 str2 = str1 + " What about you?";


10 if(str1 == str2)
11 cout << "They are equal"<< endl;
12 else
13 cout << "They are not equal"<< endl;
14

15 return 0;
16 }
Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 16, 2023 21 / 22
lab 5
• Write an efficient functions to implement strcat and strcmp
functions in C++
• Write C++ function that swap two strings
• Write C++ program to find the number of words in a given
sentence

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 16, 2023 22 / 22

You might also like