ACADEMY OF TECHNOLOGY
Lab Assignment
Subject: Programming for problem solving Discipline: B – Tech (All)
Subject Code: ES-CS291 Semester: 2nd
Assignment – 7
The objective of this assignment is to learn how to use string in the program
[Do the all programs using library function and without using library function]
1. Write a C program to find the length of a string.
[Hint: Use strlen() under <string.h> header file]
Algorithm without library function:
STEP 1: Take input in character array
STEP 2: Using a for loop, count the number of characters in the array from 0th position,
until a null character (‘\0’) is found.
2. Write a C program to count the total number of vowels and consonants in a string.
Algorithm:
STEP 1: Take input in character array
STEP 2: Initialize two variables: vowel=0 & consonant=0
STEP 3: Find the length of the string
STEP 4: Run a loop from start till end of string
STEP 5: Check if the current character is a vowel, increment vowel variable by one,
else increment consonant variable by one
3. Write a C program to concatenate two strings.
[Hint: Use strcat() under <string.h> header file]
Algorithm without library function:
STEP 1: Take 2 strings in 2 character arrays as input.
STEP 2: Find length of 1st string and store it in a variable say j.
STEP 3: Run a loop from 0 to the last character of the 2nd string.
[for(i=0;s2[i]!=’\0′;i++)]
STEP 4: Append the characters of the 2nd string at s1[j+i] position of the 1st
string until there is no character available in the 2nd string. So 2nd string is
added at the end of the 1st string.
STEP 5: Print the 1st string
4. Write a C program to find the reverse of a string.
[Hint: Use strrev() under <string.h> header file]
Algorithm without library function:
STEP 1: Take input in character array
STEP 2: Find the length of the string
STEP 3: Initialize a variable j with 1 less than the length of the input string. [As in the
reverse array, we will store the value from the 0th position.]
STEP 4: Run a loop from 0 to less than the length of input string. [for(i=0; i<count; i++)]
STEP 5: At ith position of the reverse array, store the jth positional value of the input
string.
STEP 6: Decrement j by 1 inside the loop. [will point to the next element of input string.]
STEP 7: Print the reverse array.
ACADEMY OF TECHNOLOGY | Lab Assignment
ACADEMY OF TECHNOLOGY
Lab Assignment
Subject: Programming for problem solving Discipline: B – Tech (All)
Subject Code: ES-CS291 Semester: 2nd
5. Write a C program to remove all extra blank spaces from a given string.
STEP 1: Input a string with space.
STEP 2: Find the length of the string
STEP 3: Start a loop [i] from 0 to length of string
STEP 4: Check if the current character is a space or not using [if str[i]== ‘ ‘]
STEP 5: If it’s a space, start another loop [j] from i to length of string
STEP 6: Replace space with next element [str[j] = str[j+1] ]
STEP 7: Decrement the length by 1 inside if statement
STEP 8: Print the string without space.
6. Write a C program to count the frequency of each character in a string.
STEP 1: Input a string with space.
STEP 2: Find the length of the string
STEP 3: Start a for loop [i] from 0 to length of string
STEP 4: Initialize a counter=1.
STEP 5: If string is not null start a loop [j] from i+1 to length of string
STEP 6: If(str[i] == str[j]) increment the counter, and set the current position of j with
null. [s[j]=’\0’]
[If the frequency of a character is already counted, then it will be replaced with null
character to remove duplicity of character.]
STEP 7: Print the character and frequency of that character outside the inner for loop [j],
inside the if statement.
7. Write a C program to convert lowercase string to uppercase string and vice versa.
[Hint: Use toupper() and tolower() under <string.h> header file]
8. Write a C program to compare two strings.
[Hint: Use strcmp() under <string.h> header file]
9. Write a C program to check whether a string is palindrome or not (using single string only).
10. Write a C program to find the total number of alphabets, digits or special characters in a string.
ACADEMY OF TECHNOLOGY | Lab Assignment