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

String Structure

Uploaded by

Ulugbek Kushakov
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

String Structure

Uploaded by

Ulugbek Kushakov
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

LECTURE 13

String & Structure

1
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)
2
Strings in C++
Character arrays are many time called strings. Strings
are used by programming languages to manipulate text
such as word or sentences.

A string constant is a one dimensional array of


characters terminated by a null(‘\0’).

For example:
char name[]=“Programming”;
3
Fundamentals of Strings
- 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’)
g r e e n \0

-- color can be declared also as :


char color [ ] = {‘g’, ‘r’, ‘e’, ‘e’, ‘n’, ‘\0’ };
char color [ 6] = {‘g’, ‘r’, ‘e’, ‘e’, ‘n’, ‘\0’ };
Fundamentals of Strings
- 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
g r e e n \0

-- value of variable colorPtr is the address of its first


character(g)
1. #include <iostream>
2. using namespace std;
3. int main() {
4. char name[15] = “ Final Exams";
5. cout << name<<endl; OUTPUT
6. return 0;
7. }

1. #include <iostream>
2. using namespace std;
3. int main() {
4. char name[15] = "Final Exams";
5. for (int i = 0; i < 15; i++)
6. {
7. cout << name[i];
8. }
OUTPUT
9. return 0;
10. }

6
1. #include <iostream>
2. using namespace std;
3. int main() {
4. char name[15] = "Final Exams";
5. char* ptr;
6. ptr = name;
7. int i = 0;
8. while (*ptr != '\0')
9. {
10. cout << *ptr;
11. ptr++;
12. }
13. return 0;
14. }

OUTPUT

7
1. #include <iostream>
2. using namespace std; Notice that, in the given
3. int main() { example only “Final" is
4. char name[15];
5. cout << "Enter the String:" << endl; displayed instead of “Final
6. cin >> name; Exam".
7. cout << name;
8. return 0; This is because the
9. } extraction operator >>
considers a space " " has a
terminating character.

OUTPUT

8
Fundamentals of Strings
 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 9
Fundamentals of Strings
• 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’ ); 10
C++ String to read a line
of text
1. #include <iostream>
2. using namespace std;
3. int main() {
4. char name[100];
5. cout << "Enter the String" << endl;
6. cin.get(name, 100);
7. cout << "You entered: " << name << endl;
8. return 0;
9. }

OUTPUT
String Object
1. In C++, you can also create a string object for holding strings.
2. Unlike using char arrays, string objects has no fixed length, and can
be extended as per your requirement.

1. #include <iostream>
2. #include <string>
3. using namespace std;
4. int main() {
5. string name;
6. cout << "Enter the String:" << endl;
7. getline(cin,name);
8. cout << "You entered: " << name << endl; OUTPUT
9. return 0;
10. }
String Function
1 strcpy(s1, s2): Copies string s2 into string s1.

2 strcat(s1, s2): Concatenates string s2 onto the end


of string s1.

3 strlen(s1): Returns the length of string s1.

4 strcmp(s1, s2): Returns 0 if s1 and s2 are the same;


less than 0 if s1<s2; greater than 0 if s1>s2.
String Manipulation Functions
Function Description
char *strcpy(char *s1, const char *s2); Copies string s2 into the
character array s1. The
value of s1 is returned.
char *strncpy(char *s1, const char *s2, size_t n); Copies at most n
characters of string s2 into
the array s1. The value of
s1 is returned.
char *strcat (char *s1, const char *s2); Appends string s2 to
string s1. The value of s1
is returned.
char *strncat (char *s1, const char *s2, size_t n); Appends at most n
characters of string s2 to
string s1. The value of s1
is returned.
14
String Manipulation Functions

int strcmp(const char *s1, const char *s2); 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 strncmp(const char *s1, const char *s2, size_t n); 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.
15
String Manipulation Functions

Size_t strlen( const char *s); Determines the length of string s. The
number of characters preceding the
terminating null character is returned.

16
String Manipulation Functions
1- strcpy(s1, s2)  s1 = s2 Copies string s2 into
string s1.
1. #include <iostream>
2. #include <cstring>
3. using namespace std;

4. int main() {
5. char str1[] = "HELLO";
6. const char* str2 = "IUT"; // str2 is assigned a string literal

7. strcpy(str1, str2); // Copy content of str2 into str1

8. cout << str1 << endl; // Output the copied string

9. return 0;
10. }
2- strncpy(s1, s2)  s1[n] = s2[n]
1. #include <iostream>
2. #include <cstring>
3. using namespace std;

4. int main() {
5. char str1[] = " **********"; // A character array with an initial
string of 10 spaces
6. const char* str2 = "$$$$$$$$$$"; // A constant character pointer
pointing to a string "$$$$$$$$$$"

7. strncpy(str1, str2, 5); // Copy the first 5 characters of str2 into


str1

8. cout << str1 << endl; // Print str1

9. return 0;
10. }
3- strcat(s1, s2)  s1 = s1+s2
Concatenates string s2 onto the end of string s1.
1. #include <iostream>
2. #include <cstring>
3. using namespace std;

4. int main() {
5. char str1[24] = “INHA"; // Initialize str1 with the string “Inha"
6. const char* str2 = "University"; // Initialize str2 with the string
"University"

7. strcat(str1, str2); // Concatenate str2 to the end of str1

8. cout << str1 << endl; // Print the result stored in str1

9. return 0;
10. }
4- strncat(s1, s2,n)  s1 = s1+s2[n]
1. #include <iostream>
2. #include <cstring>
3. using namespace std;

4. int main() {
5. char str1[24] = “INHA"; // Initialize str1 with the string “Inha"
6. const char* str2 = "University"; // Initialize str2 with the string
"University"

7. strncat(str1, str2,10); // Concatenate str2 to the end of str1

8. cout << str1 << endl; // Print the result stored in str1

9. return 0;
10. }
4- strncat(s1, s2,n)  s1 = s1+s2[n]
1. #include <iostream>
2. #include <cstring>
3. using namespace std;

4. int main() {
5. char str1[24] = “INHA"; // Initialize str1 with the string “Inha"
6. const char* str2 = "University"; // Initialize str2 with the string
"University"

7. strncat(str1, str2,10); // Concatenate str2 to the end of str1

8. cout << str1 << endl; // Print the result stored in str1

9. return 0;
10. }
5- strcmp(s1, s2) 0 if s1 = s2  -1 if s1 < s2
 1 if s1 > s2
1. #include <iostream>
2. #include <cstring>
3. using namespace std;

4. int main() {
5. char str1[20]; // Declare a character array with 20 characters for
str1
6. char str2[20]; // Declare a character array with 20 characters for
str2
7. cin.getline(str1, 20); // Read a line of text into str1, with a
maximum of 19 characters (20th is for null terminator)
8. cin.getline(str2, 20); // Read a line of text into str2, with a
maximum of 19 characters (20th is for null terminator)
9. if (strcmp(str1, str2)) // Compare str1 and str2 using strcmp
10. if (strcmp(str1, str2) == 1) // If str1 > str2 (returns positive
value)
11. cout << str1 << " > " << str2 << endl; // Print str1 > str2
12. else // If str1 < str2 (returns negative value)
13. cout << str1 << " < " << str2 << endl; // Print str1 < str2
14. else // If str1 == str2 (strcmp returns 0)
15. cout << str1 << " = " << str2 << endl; // Print str1 = str2
16. return 0;
17. }
5- strcmp(s1, s2) 0 if s1 = s2  -1 if s1 < s2
 1 if s1 > s2
1. #include <iostream>
2. #include <cstring>
3. using namespace std;

4. int main() {
5. char str1[20]; // Declare a character array with 20 characters for
str1
6. char str2[20]; // Declare a character array with 20 characters for
str2
7. cin.getline(str1, 20); // Read a line of text into str1, with a
maximum of 19 characters (20th is for null terminator)
8. cin.getline(str2, 20); // Read a line of text into str2, with a
maximum of 19 characters (20th is for null terminator)
9. if (strcmp(str1, str2)) // Compare str1 and str2 using strcmp
10. if (strcmp(str1, str2) == 1) // If str1 > str2 (returns positive
value)
11. cout << str1 << " > " << str2 << endl; // Print str1 > str2
12. else // If str1 < str2 (returns negative value)
13. cout << str1 << " < " << str2 << endl; // Print str1 < str2
14. else // If str1 == str2 (strcmp returns 0)
15. cout << str1 << " = " << str2 << endl; // Print str1 = str2
16. return 0;
17. }
6- strncmp(s1, s2,n) 0 if s1[n] = s2[n]  -1 if s1[n]
< s2[n]  1 if s1[n] > s2[n]
1. #include <iostream>
2. #include <cstring>
3. using namespace std;
4. int main() {
5. char str1[20];
6. char str2[20];
7. cin.getline(str1, 20);
8. cin.getline(str2, 20);
9. if (strncmp(str1, str2, 1))
10. if (strncmp(str1, str2,1) == 1)
11. cout << str1 << " > " << str2 << endl;
12. else
13. cout << str1 << " < " << str2 << endl;
14. else
15. cout << str1 << " = " << str2 << endl;
16. return 0;
17. }
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
1. #include <iostream>
2. #include <cstring>
3. using namespace std;

4. int main() {
5. char s1[] = "Tashkent"; // Declare a character array and initialize
with the string "Tashkent"
6. const char* s2 = "New York"; // Declare a pointer to a constant
string and initialize with "New York"

7. cout << s1 << " Consists of " << strlen(s1) << " Characters.\n"; //
Print the length of s1
8. cout << s2 << " Consists of " << strlen(s2) << " Characters.\n"; //
Print the length of s2

9. return 0;
10. }

You might also like