0% found this document useful (0 votes)
12 views5 pages

Strings Eaxmples

Uploaded by

Vimal EA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

Strings Eaxmples

Uploaded by

Vimal EA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Find the Frequency of a Character

Scenario:
Find how many times a specific character appears in a given string.

Program:

#include <stdio.h>

int main() {

char str[100], target;

int count = 0;

printf("Enter a string: ");

scanf("%s", str);

printf("Enter the character to find: ");

scanf(" %c", &target);

for (int i = 0; str[i] != '\0'; i++) {

if (str[i] == target) {

count++;

printf("The character '%c' appears %d times in the string.\n", target, count);

return 0;

}
. Compare Two Strings

Scenario:
Check if two strings are equal or not.

Program:

#include <stdio.h>

int main() {

char str1[100], str2[100];

int areEqual = 1;

printf("Enter the first string: ");

scanf("%s", str1);

printf("Enter the second string: ");

scanf("%s", str2);

for (int i = 0; str1[i] != '\0' || str2[i] != '\0'; i++)


{
if (str1[i] != str2[i])
{
areEqual = 0;
break;
}
}

if (areEqual) {

printf("The strings are equal.\n");

} else {

printf("The strings are not equal.\n");

return 0;

}
Reverse a String

Scenario:
Write a program to reverse the characters of a given string.

Program:

#include <stdio.h>

int main()

char str[100], reversed[100];

int length = 0;

printf("Enter a string: ");

scanf("%s", str);

// Calculate length

while (str[length] != '\0') {

length++;

// Reverse the string

for (int i = 0; i < length; i++)

reversed[i] = str[length - i - 1];

reversed[length] = '\0'; // Null-terminate the reversed string

printf("Reversed string: %s\n", reversed);

return 0;

}
Count Vowels and Consonants
Scenario:
Count the number of vowels and consonants in a given string.
Program:

#include <stdio.h>

int main() {

char str[100];

int vowels = 0, consonants = 0;

printf("Enter a string: ");

scanf("%s", str);

for (int i = 0; str[i] != '\0'; i++)

char ch = str[i];

if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||

ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')

vowels++;

else

consonants++;

printf("Vowels: %d\n", vowels);

printf("Consonants: %d\n", consonants);

return 0;

}
**************************************************************************************

Real time Scenario based questions


Password Strength Checker

Scenario:
Check if a password is strong. A password is strong if it contains:

• At least one uppercase letter.

• At least one digit.

• At least one special character (@, #, !, etc.).

Username Validation

Scenario:
Validate a username based on these rules:

• Must not contain spaces.

• Must be between 5 and 20 characters long.

Count Words in a Sentence

Scenario:
Count the number of words in a given sentence. Words are separated by spaces.

Find Frequency of a Character in a String

Scenario:
Find how many times a specific character appears in a string.

**************************************************************************************

You might also like