Write a code to check whether the two strings are anagram of each other or not.

Q15. Write a code to check whether the two strings are anagram of each other or not.

Anagram :- When two strings contain the same characters irrespective of the order of occurrence then such strings are known as Anagram.
For Example :-
String1 = eleven plus two
String2 = twelve plus one
So as we can see that both strings have same characters, only the order of characters is different so this is an Anagram

Algorithm for Anagram

START
Step->1 : Define two strings.
Step->2 : Check for their lengths. If the lengths are not equal, then strings are not an anagram.
Step->3 : Else, convert the string to lower case character to make the comparison easy.
Step->4 : Some language allows the strings to provide inbuilt function for sorting of string. If not then convert them to character array for sorting.
Step->5 : Sort the array.
Step->6 : Finally, check for the equality of content.
STOP

Code for Anagram

Output

The two strings are anagram of each other

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number

Write a code to check whether the given number is palindrome or not

Q14. Write a code to check whether the given number is palindrome or not

Palindrome Number:- A Palindrome Number is a number that remains same when its digits are reversed or we can say that the Palindrome number is a number that is symmetric.

For Example:- 121
So as we can see that the number 121 remains the same when it is reversed so it is a palindrome number.

Algorithm for Palindrome Number

START
Step 1 : Get the number to check for palindrome
Step 2 : Hold the number in temporary variable
Step 3 : Reverse the number
Step 4 : Compare the temporary number with reversed number
Step 5 : If both numbers are same, print “palindrome number”
Step 6 : Else print “not palindrome number”
STOP

Code for Palindrome Number

Output

The number 121 is a Palindrome number

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number

Write a code to find length of a string

Q13. Write a code to find length of a string

String :- The string is defined as an array of characters. The difference between a character array and a string is the string ending with a special character ‘\0’.

For Eg :- If the string is “alfatechlab”, the output is 11.

Algorithm for find length of string

START
Step 1:Length = 0
Step 2: Repeat step 3 while S1 [Length] ≠ NULL
Step 3: Length = Length + 1
Step 4: Return Length
STOP

Code for find length of string

Output

Length of the string =11

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number

Write a code to sort the characters in a given string

Q12. Write an code and algorithm to sort string of characters in dictionary order

Solution : Given a string contain lowercase character from a to z and our task is to sort them in dictionary order.

String :- A string is a sequence of characters terminated with a null character \0 .
For example: char lfc[] = “alfatechlab”;

Algorithm for sort string of characters in dictionary order

//Algorithm To Sort String Of Characters
START
Step 1 -> Take string as input str.
Step 2 -> Store the count of each character in one array ARR, index 0 corresponding to a and so on.
Step 3 -> Now repeat loop I till the end of array ARR
Step 4 -> Repeat inner loop on the basis of value of ARR[I]
Step 5 -> Print (‘a’ + I)
Step 6 -> End Inner loop
Step 7 -> End Loop
STOP

Code  for sort string of characters in dictionary order

Output

aaabcefhllt

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number

Write a code to reverse a string

Q11. Write a code to reverse a string

Given a string and the task is to write a algorithm and program to reverse a string. A string is sequence of characters terminated with a null character \0. So we are given a input string and we need to print its reverse order.
For Example: We have given input string as “ALFATECHLAB” and it’s reverse output we have to find out.
Input=”ALFATECHLAB”
Output=”BALHCETAFLA”

Explanation:
1. First declare a string variable and initialize an empty string to “ALFATECHLAB”.

2. Now we have to calculate the length of the string which we can do using loop(increment the value of count until the string is \0) or using the in-build function(strlen function).

3. Define two variables start = 0 and end = length -1.(Suppose “ALFATECHLAB” has a length of 11, then we will swap the 0th character with the 10th character, the 1st character with the 9th character, and the loop will iterate until length / 2 = 5).

4. Iterate the loop from i = 0 to i = length / 2 and increment the variable i by 1.

5. As we already define the start and end variable values, so now we just have to swap the characters one by one and increment the value of start += 1 and decrement the value of end- = 1 during each iteration.

6. After completing iteration print the string and output will be “BALHCETAFLA”.

Algorithm For Reverse A String

//Algorithm To Reverse A String
START
Step 1 -> Input string str1.
Step 2 -> calculate the length of the string.
Step 3 -> define start=0 and end=length-1;
Step 4 -> for i=0 to length/2.
temp=str1[start];
str1[start]=str1[end];
str1[end]=temp;
start+=1;
end-=1;
Loop End
step 5-> Display String str1.
STOP

Code For Reverse A String

Output

orip ni gnidoc

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number