Find the Frequency of a Character
#include <stdio.h>
int main() {
char str[1000], ch;
int count = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
printf("Enter a character to find its frequency: ");
scanf("%c", &ch);
for (int i = 0; str[i] != '\0'; ++i) {
if (ch == str[i])
++count;
printf("Frequency of %c = %d", ch, count);
return 0;
Program to count vowels, consonants, etc.
#include <ctype.h>
#include <stdio.h>
int main() {
char line[150];
int vowels, consonant, digit, space;
// initialize all variables to 0
vowels = consonant = digit = space = 0;
// get full line of string input
printf("Enter a line of string: ");
fgets(line, sizeof(line), stdin);
// loop through each character of the string
for (int i = 0; line[i] != '\0'; ++i) {
// convert character to lowercase
line[i] = tolower(line[i]);
// check if the character is a vowel
if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' ||
line[i] == 'o' || line[i] == 'u') {
// increment value of vowels by 1
++vowels;
// if it is not a vowel and if it is an alphabet, it is a consonant
else if ((line[i] >= 'a' && line[i] <= 'z')) {
++consonant;
}
// check if the character is a digit
else if (line[i] >= '0' && line[i] <= '9') {
++digit;
// check if the character is an empty space
else if (line[i] == ' ') {
++space;
printf("Vowels: %d", vowels);
printf("\nConsonants: %d", consonant);
printf("\nDigits: %d", digit);
printf("\nWhite spaces: %d", space);
return 0;
Calculate Length of String without Using
strlen() Function
#include <stdio.h>
int main() {
char s[] = "Programming is fun";
int i;
for (i = 0; s[i] != '\0'; ++i);
printf("Length of the string: %d", i);
return 0;
Finding the Length of a String:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello World";
size_t length = strlen(str); // strlen() returns the length excluding the null terminator
printf("Length of the string: %zu\n", length);
return 0;
Copying a String:
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Copy this string";
char destination[50]; // Ensure destination has enough space
strcpy(destination, source); // Copies source to destination
printf("Copied string: %s\n", destination);
return 0;
Concatenating Strings:
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello, ";
char str2[] = "World!";
strcat(str1, str2); // Appends str2 to str1
printf("Concatenated string: %s\n", str1);
return 0;
}
Searching for a Character:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "programming";
char *ptr = strchr(str, 'g'); // Finds the first occurrence of 'g'
if (ptr != NULL) {
printf("Character 'g' found at position: %ld\n", ptr - str);
} else {
printf("Character 'g' not found.\n");
}
return 0;
}
Searching for a Substring:
#include <stdio.h>
#include <string.h>
int main() {
char text[] = "This is a sample string.";
char *sub = strstr(text, "sample"); // Finds the first occurrence of "sample"
if (sub != NULL) {
printf("Substring found: %s\n", sub);
} else {
printf("Substring not found.\n");
}
return 0;
}