Untitled
#include <stdio.h> #include <string.h> #include <stdbool.h> /* Take a string input from the user Implement the following functions 1. Change the case of the given string --> Hi ==> hI 2. Count the no.of cosonants present (both lower and upper) 3. Count the number of words in the input string 4. Check if the given string is palindrome or not 5. Check if the given character is present in the string 6. Find the given character and replace it with the other given character 7. Check if the string contains the given substring or not */ void ToggleString(char s[]) { for(int i=0;i<strlen(s);i++) { //check if the character is an aplhabet or not if((s[i]>=65 && s[i]<=90) || (s[i]>=97 && s[i]<=122)) { //change to uppercase if(s[i]>=97 && s[i]<=122) { s[i] = s[i]-32-'\0'; } //change to lowercase else if(s[i]>=65 && s[i]<=90) { s[i] = s[i]+32; } } } printf("String after changing the case : %s\n", s); } void CountConsonants(char s[]) { int count = 0; for(int i=0;i<strlen(s);i++) { //consonant count if((s[i]!='a' && s[i]!='e' && s[i]!='i' && s[i]!='o' && s[i]!='u') && (s[i]!='A' && s[i]!='E' && s[i]!='I' && s[i]!='O' && s[i]!='U')) { count++; } } printf("Consonant count : %d\n", count); } void CountWords(char s[]) { int count_words = 0; for(int i=0;i<strlen(s);i++) { //if space is found increment the count if(s[i]==' ') { count_words++; } //if the character is space or the index position is the end increment the count if(s[i]=='\0' || i == strlen(s)-1) { count_words++; } } printf("Word count : %d\n", count_words); } void CheckPalindrome(char s[]) { int end = strlen(s)-1, count = 0; //iterate half of the string for(int i=0; i<strlen(s)/2; i++) { //check if the first and last characters are same if(s[i] == s[end]) { count++; end--; } } //if count is equal to half of the length, it is palindrome if(count == strlen(s)/2) { printf("Given string is plaindrome\n"); } else { printf("Given string is not a plaindrome\n"); } } void FindChar(char s[], char ch) { //a flag variable bool char_found = false; for(int i=0;i<strlen(s);i++) { if(s[i] == ch) { //if the character is found reset the flag char_found = true; } } if(char_found) { printf("Given character found\n"); } else { printf("Given character not found\n"); } } void FindReplace(char s[], char c1, char c2) { for(int i=0;i<strlen(s);i++) { if(s[i] == c1) { //replace the character with given character s[i] = c2; } } printf("After replacing : %s\n", s); } void FindSubstring(char s[], char substring[]) { char result[strlen(substring)]; int index = 0, res = 0, count = 0; for(int i=0;i<strlen(s);i++) { for(int j=i;j<i+strlen(substring);j++) { //append each character to result array result[index] = s[j]; index++; } for(int k=0;k<strlen(substring);k++) { //check if result is same as substring and increment the res varaiable if(substring[k] == result[k]){ res++; } } // if the res and substring length are equal then increment count if(res == strlen(substring)) { count++; break; } //reset the index to 0 after every substring index = 0; } //if a sbustring is found the count is greater than 0 if(count>0) { printf("Substring is found\n"); } else { printf("Substring not found\n"); } } int main() { char s[100]; scanf("%[^\n%]s", s); printf("String : %s\n", s); ToggleString(s); CountConsonants(s); CountWords(s); CheckPalindrome(s); FindChar(s,'h'); FindReplace(s, 'h', 'H'); FindSubstring(s, "aLL"); }
Leave a Comment