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

Data Structure and Algorithms Unit-2 Strings

This ppt contains notes for diploma computer engineering students.

Uploaded by

rutvi sheth
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)
338 views

Data Structure and Algorithms Unit-2 Strings

This ppt contains notes for diploma computer engineering students.

Uploaded by

rutvi sheth
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/ 20

Data Structure and

Algorithm
4330704
Rutvi S. Sheth
Unit-2 Strings
CO1: Perform basic operations on arrays and strings.
2.1 String representation: Reading and Writing Strings

• String: It is sequence of collection of characters.


• In terms of c language string is an array of characters.
• The string terminated with NULL or “\0” is known as null terminated string.
• For example: To store “HELLO” in array the array must be declared as char[5].
• String “HELLO” is stored as shown in fig
• For example:
H E L L O \0
2.1 String representation: Reading and Writing Strings
• String Character set:
o Lower case: a to z
o Upper case: A to Z
o Number: 0 to 9
o Special Characters: + - * % / ( ) [ ] { } $ # &, . ? @ Etc.
• Declaration of String
o Char string_Name[Size];
o Char str[10];
2.1 String representation: Reading and Writing Strings
• Initialization of String
o Char str[10]=”Hello”;
o Char str[10]={‘H’,’e’,’l’,’l’,’o’};
o Char str[10];
o scanf(“%s”,str);
• getchar(): It is used to get a single character from the terminal.
o Example: char str;
o str=getchar();
• gets(): The function read line of, containing whitespace until the new line character.
o Example: char str[10];
o gets(str);
o printf(“%s”,str);
2.1 String representation: Reading and Writing Strings
• putchar(): It is used to put a single character on the terminal.
• Example:
o putchar(str);
• puts(): The function print line of, containing whitespace until the new line character.
o Example: char str[10]=”Hello”;
o puts(str);
2.2 String operations
1. String Length: This function finds the length of the string.
2. Uppercase: Returns string characters in uppercase.
3. Lowercase: Returns string characters in lowercase.
4. String Concat : This function concats two strings and store it in to the another string.
5. String Append: It is used to append a given string str1 to another specified string.
6. Reverse string: This operation is used to reverse the given string.
7. String Copy: This function copy one string in to another string.
8. String Compare: This function compare two strings.
9. Insertion: Is used to insert characters in string at specified
10. Substring: This function finds one string into another string.
11. Deletion: Is used to delete characters in string at specified position.
Write an algorithm to find length of string.
This algorithm counts the length of the given string.
Algorithm: STR_LEN(str)
str : given string.
i : index pointer to str
Step: 1 [Initialization] i←0
Step: 2 [Read String] Read(str)
Step: 3[Process until end of the string]
Repeat while (str[i]! = NULL) i←i + 1
Step: 4 [Print length]
Write(“Length of string: i”)
Step: 5 [Finished]
Exit.
Write an algorithm to convert characters of string into uppercase.

STR_UPPER (str1,str2) Step: 3 [Convert lowercase to Uppercase]


str1 : given string1. Repeat while ( str1[i] != ‘\0’)
str2 : converted string2. if(str1[i] >= ‘a’ and str1[i] <= ‘z’)
i : index pointer to str1
str2[j] ← str1[i] -32
j : index pointer to str2
Step: 1 [Initialization] else
str2[j] ← str1[i]
i←0, j ← 0
i ← i + 1 , j ← j+1
Step: 2 [Read String]
Step: 4 [Print the Uppercase String]
Read (str1) str2[j] ← ’\0’
Write (str2)
Step: 5 [Finished]
Exit.
Write an algorithm to convert characters of string into lowercase.

This algorithm counts the length of Step: 3 [Convert Uppercase to lowercase]


the given string. Repeat while ( str1[i] != ‘\0’)
STR_LOWER (str1,str2) if(str1[i] >= ‘A’ and str1[i] <= ‘Z’)
str1 : given string1. str2[j] ← str1[i] +32
str2 : converted string2. else
i : index pointer to str1 str2[j] ← str1[i]
j : index pointer to str2 i ← i + 1 , j ← j+1
Step: 1 [Initialization] Step: 4 [Print the Lowercase String]
i←0, j ← 0 str2[j] ← ’\0’
Step: 2 [Read String] Write (str2)
Read (str1) Step: 5 [Finished]
Exit.
Write an algorithm for string concatenation (str3=str1+str2)
Algorithm: STR_CONCATE(str1,str2,str3) Step: 3 [Copy String1 into String3]
str1 : given string1. Repeat while (str1[i] != ‘\0’)
str2 : given string2. str3[k] ← str1[i]
str3 : Concatenated String3 . i←i+1
i : index pointer to str1 k←k+1
j : index pointer to str2 Step: 4 [Copy String2 into String3]
k : index pointer to str3
Repeat while (str2[j] != ‘\0’)
Step: 1 [Initialization] str3[k] ← str2[j]
i←0, j ← 0 , k ← 0 , str3 ←Null j←j+1
Step: 2 [Read String] k←k+1
Read (str1) , Read (str2) Step: 5 [Finished]
Exit.
Write an algorithm to append string (str1=str1+str2)
Algorithm: STR_APPEND(str1,str2) Step: 4 [Append String]
str1 : given string1. Repeat while (str2[j] != ‘\0’)
str2 : given string2. str1[i] ← str2[j]
i : index pointer to str1 i← i + 1
j : index pointer to str2 j← j + 1
Step: 1 [Initialization]
i←0, j ← 0 Step: 5 [Print the string after Append operation
Step: 2 [Read String] performed]
Read (str1) , Read (str2) str1[i] ←’\0’
write(str1)
Step: 3 [Reach at end of string1]
Repeat while (str1[i] != ‘\0’) Step: 6 [Finished]
i ← i +1 Exit.
Write an algorithm to reverse a string
Algorithm: STR_REVERSE(str1,str2) Step: 4 [Store Reverse string from Original String]
str1 : given string1. i ← i -1
str2 : reverse string2. Repeat while (i>=0)
i : index pointer to str1 str2[j] ← str1[i]
j : index pointer to str2 i← i - 1
Step: 1 [Initialization] j← j + 1
i←0, j ← 0, str2 ←Null
Step: 2 [Read String] Step: 5 [Print the reverse string]
Read (str1) str2[j] ←’\0’
Step: 3 [Reach at end of string1] write(str2)
Repeat while (str1[i] != ‘\0’)
Step: 6 [Finished]
i ← i +1
Exit.
Write an algorithm to copy one string into another string
Algorithm: STR_COPY(str1,str2) Step: 3 [Copy Operation Performed]
str1 : given string1. Repeat while (str1[i]!=‘\0’ )
str2 : new copied string2. str2[j] ← str1[i]
i : index pointer to str1 i← i + 1
j : index pointer to str2 j← j + 1
Step: 1 [Initialization]
i←0, j ← 0, str2 ←Null Step:4 [Print the string after copy operation
performed]
Step: 2 [Read String] str2[j] ←’\0’
Read (str1) write(str2)

Step: 6 [Finished]
Exit.
STRING COMPARISON
• We have two string, str1 and str2.
• Compare str1 and str2 character by character.
• If both are same then give result “Equal”.
• If both are different then give result “Not equal”.
• Example: str1=”computer” and str2=”computer”, then both strings are equal.
• If str1=”computer” and str2=”Comp” then strings are not equal.
Write an algorithm for string comparison
Algorithm: STR_COMPARE(str1,str2) Step: 4 [Check the length of both strings]
str1 : given string1. If (L1! = L2)
str2 : new copied string2. Write (“Both strings are different”)
i : index pointer to str1 Exit
j : index pointer to str2 Step: 5 [Compare two string character by character]
L1: length of string1 Repeat while (str1 [i] != ‘\0’)
L2: length of string2 if(str1[i]!= str2[j])
Step: 1 [Initialization] Write (“Both Strings are different”)
i←0, j ← 0 Exit
Step: 2 [Read two Strings] else
i ← i+1 , j ← j+1
Read (str1) , Read (str2)
Step: 6 [ Equal string]
Step: 3 [Find Length of two strings] Write (“Both Strings are Equal”)
L1 ← strlen(str1), L2 ← strlen(str2) Step: 7 [Finished]
Exit.
Write an algorithm for string insertion
Algorithm: STR_INSERTION(str1,str2,str3) Repeat while (i != pos -1)
str3[k] ← str1[i]
str1 : given string1.
i←i+1
str2 : string2 to insert.
k←k+1
str3: new string
Repeat while (str2 [j]! = ‘\0’)
i : index pointer to str1
str3 [k] ← str2[j]
j : index pointer to str2
j←j+1
k: index pointer to str3
k←k+1
Pos: position to insert
Repeat while (str1[i] != ‘\0’)
Step: 1 [Initialization]
str3[k] ← str1[i]
i←0, j ← 0, k ←0
i←i+1
Step: 2 [Read two Strings and pos] k←k+1
Read (str1) , Read (str2) , Read (pos) Step: 4 [Print the string]
Step: 3 [To reach at position for insert] str3 [k] ← ‘\0’ write (str3)
Step: 5 [Finished]
Exit.
Process to find substring
• We have two string, str1 and str2.
• Read string 1 from 2nd position and print total 3 characters.
• str1 : given string1. H E L L O \0
• str2: new string L L O \0
• num: Position for substring
• total: number of character to read
Write an algorithm for substring
Algorithm: STR_SUBSTRING(str1,str2) i ← pos
str1 : given string1. Repeat while (total> 0)
str2 : new string str2[j] ← str1[i]
pos: Position for substring i←i+1
total: number of character to read j←j+1
i: index pointer to string 1 total ← total – 1
j: index pointer to string 2
Step: 1 [Initialization] Step: 4 [Print the string]
i←0, j ← 0, str2 ←Null str2 [j] ← ‘\0’
write (str2)
Step: 2 [Read String1, position and total no
of characters]
Step: 5 [Finished]
Read (str1) , Read (pos) , Read (total) Exit.
Step: 3 [Process for substring]
Write an algorithm for string Deletion.
Algorithm: STR_DELETION(str1,str2) Repeat while (i! = position -1)
str1 : given string str2[j] ← str1[i]
str2 : new string i←i+1
pos: Position for deleting j←j+1
total: number of character to delete
i: index pointer to string1 Step: 4 [Reset new value of i after delete character]
j: index pointer to string2 i ← i + total
Step: 1 [Initialization] Step: 5 [Copy remaining string]
i←0, j ← 0, str2 ←Null Repeat while (str1 [i]! = NULL)
str2 [j] ← str1 [i]
Step: 2 [Read String1, position and total no
i←i+1
of characters]
j ← j+ 1
Read (str1) , Read (position) , Read (total) Step: 6 [Finished]
Step: 3 [To reach at position for deletion] Exit.

You might also like