Practical No: 2(A)
Practical Title: Write a python program to perform String operations.
Aim: Write a Python program to compute following operations on String:
a) To display word with the longest length
b) To determines the frequency of occurrence of particular character in the string
c) To check whether given string is palindrome or not
d) To display index of first appearance of the substring
e) To count the occurrences of each word in a given string
Pre-requisite:
• Basics of String operations.
Objectives:
To understand the use standard library functions for string operations. To perform
the string operations.
Input: One or Two Strings
Output: Resulting string after performing string operation.
Theory :
String:
String is defined as an array of characters or a pointer to characters.
Null-terminated String:
String is terminated by a special character which is called as null terminator or
null parameter (\0). So when you define a string you should be sure to have
sufficient space for the null terminator. The null terminator has value 0.
Declaring String:
As in string definition, we have two ways to declare a string. The first way
is, we declare an array of characters as follows:
char s[] = “string”
or
char str[20];
String operation (explain each operation in detail with example)
a) To dispslay word with the longest length
b) To determines the frequency of occurrence of particular character in the string
c) To check whether given string is palindrome or not
d) To display index of first appearance of the substring
e) To count the occurrences of each word in a given string
Algorithms:
A) To check whether given string is palindrome or not
Input the string.
Find the reverse of the string.
If the reverse of the string is equal to the input string, then return true. Else,
return false.
B) To determines the frequency of occurrence of particular character in the
strings
First, we split the string by spaces in a
Then, take a variable count = 0 and in every true condition we increment the
count by 1
Now run a loop at 0 to length of string and check if our string is equal to the
word
if condition is true then we increment the value of count by 1 and in the end,
we print the value of count.
C) Count the occurrences of each word in a given string
Conclusion:
By this way, we can perform string operations successfully.
Questions:
1. Write a program to find the length of string.
2. Write a program to display string from backward.
3. Write a program to count number of words in string.
4. Write a program to concatenate one string contents to another.
5. Write a program to compare two strings they are exact equal or not.
6. Write a program to check a string is palindrome or not.
7. WAP to find a substring within a string. If found display its starting position.
8. Write a program to reverse a string.
9. Write a program to convert a string in lowercase.
10. Write a program to convert a string in uppercase.