1. Program to Count Vowels, Consonants, Digits, and Spaces in a Given String (using scanf
)
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int vowels = 0, consonants = 0, digits = 0, spaces = 0;
printf("Enter a string: ");
scanf("%[^\n]s", str); // Using %[^\n] to read the whole line including spaces
for (int i = 0; str[i] != '\0'; i++) {
char ch = tolower(str[i]);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowels++;
} else if (isalpha(ch)) {
consonants++;
} else if (isdigit(ch)) {
digits++;
} else if (isspace(ch)) {
spaces++;
}
}
printf("Vowels: %d\n", vowels);
printf("Consonants: %d\n", consonants);
printf("Digits: %d\n", digits);
printf("Spaces: %d\n", spaces);
return 0;
}
2. Program to Remove All Vowels from a Given String (using scanf
)
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100], result[100];
int j = 0;
printf("Enter a string: ");
scanf("%[^\n]s", str); // Using %[^\n] to read the whole line including spaces
for (int i = 0; str[i] != '\0'; i++) {
char ch = tolower(str[i]);
if (!(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')) {
result[j++] = str[i];
}
}
result[j] = '\0'; // Null terminate the result string
printf("String without vowels: %s\n", result);
return 0;
}
3. Program to Check Whether a Given String is a Palindrome
#include <stdio.h>
#include <string.h>
int main() {
char str[100], rev[100];
int length, flag = 1;
printf("Enter a string: ");
scanf("%[^\n]s", str);
length = strlen(str);
for (int i = 0; i < length; i++) {
rev[i] = str[length - i - 1];
}
rev[length] = '\0'; // Null terminate the reversed string
for (int i = 0; i < length; i++) {
if (tolower(str[i]) != tolower(rev[i])) {
flag = 0;
break;
}
}
if (flag)
printf("The string is a palindrome.\n");
else
printf("The string is not a palindrome.\n");
return 0;
}
4. Program to Convert a Given String to Uppercase Without Using strupr()
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
printf("Enter a string: ");
scanf("%[^\n]s", str);
for (int i = 0; str[i] != '\0'; i++) {
str[i] = toupper(str[i]);
}
printf("Uppercase string: %s\n", str);
return 0;
}
5. Program to Find the Frequency of Each Character in a String
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int freq[256] = {0};
printf("Enter a string: ");
scanf("%[^\n]s", str);
for (int i = 0; str[i] != '\0'; i++) {
freq[(unsigned char)str[i]]++;
}
printf("Character frequencies:\n");
for (int i = 0; i < 256; i++) {
if (freq[i] != 0) {
printf("%c: %d\n", i, freq[i]);
}
}
return 0;
}