0% found this document useful (0 votes)
57 views12 pages

#Include Int Main (Char STR (100) Int I 0 : Program To Print Characters in A String

The document contains C program code snippets that demonstrate various string manipulation tasks, including: 1. Printing each character in a string 2. Counting the number of words in a string 3. Counting the number of vowels, consonants, digits, and spaces in a string 4. Removing all non-alphabetic characters from a string 5. Finding the frequency of a character in a string 6. Finding the ASCII value of a character 7. Counting punctuation characters in a string 8. Determining if two strings are anagrams by comparing their sorted characters.

Uploaded by

ramesh online
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)
57 views12 pages

#Include Int Main (Char STR (100) Int I 0 : Program To Print Characters in A String

The document contains C program code snippets that demonstrate various string manipulation tasks, including: 1. Printing each character in a string 2. Counting the number of words in a string 3. Counting the number of vowels, consonants, digits, and spaces in a string 4. Removing all non-alphabetic characters from a string 5. Finding the frequency of a character in a string 6. Finding the ASCII value of a character 7. Counting punctuation characters in a string 8. Determining if two strings are anagrams by comparing their sorted characters.

Uploaded by

ramesh online
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/ 12

Program to Print Characters in a String

#include <stdio.h>
int main()
{
char str[100];
int i = 0;

printf("\n Please Enter any String : ");


scanf("%s", str);

while (str[i] != '\0')


{
printf("The Character at %d Index Position = %c \n", i, str[i]);
i++;
}
return 0;
}
Q) WRITE A PROGRAM TO PRINT NO OF WORDS.
#include <stdio.h>
#include <string.h>
int main()
{
int i,j;
char word[50];
char str[50]="PRR TECHNOLOGIES";
int len1=1;
int count=1;
for(i=0;str[i]!='\0';i++)
{
if(str[i]==' '||str[i]== '\t'||str[i]== '\n')
{
count++;
}
}
printf("no. of words %d",count);
}
Program to count vowels, consonants, digits and numbers.
#include <stdio.h>
int main() {

char line[150];
int vowels, consonant, digit, space;

vowels = consonant = digit = space = 0;


printf("Enter a line of string: ");
fgets(line, sizeof(line), stdin);

line[i] = tolower(line[i]);

if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' ||


line[i] == 'o' || line[i] == 'u') {

++vowels;
}

else if ((line[i] >= 'a' && line[i] <= 'z')) {


++consonant;
}

else if (line[i] >= '0' && line[i] <= '9') {


++digit;
}

else if (line[i] == ' ') {


++space;
}
}
printf("Vowels: %d", vowels);
printf("\nConsonants: %d", consonant);
printf("\nDigits: %d", digit);
printf("\nWhite spaces: %d", space);

return 0;
}
Q) C Program to Remove all Characters in a String Except
Alphabets
#include <stdio.h>
int main() {
char line[150];

printf("Enter a string: ");


gets(line);

for (int i = 0, j; line[i] != '\0'; ++i) {


while (!(line[i] >= 'a' && line[i] <= 'z') && !(line[i] >= 'A' && line[i] <=
'Z') && !(line[i] == '\0')) {
for (j = i; line[j] != '\0'; ++j) {

// if jth element of line is not an alphabet,


// assign the value of (j+1)th element to the jth element
line[j] = line[j + 1];
}
line[j] = '\0';
}
}
printf("Output String: ");
puts(line);
return 0;
}
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;
}
C Program to Find ASCII Value of a Character
#include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf("%c", &c);

// %d displays the integer value of a character


// %c displays the actual character
printf("ASCII value of %c = %d", c, c);

return 0;
}
Program to count the total number of punctuation characters
exists in a string.
#include <stdio.h>
int main ()
{
int i, countPuncMarks = 0;
char str [] = "Good Morning! Mr. Laddu Ziddu. Had, your breakfast?";

//Checks whether given character is punctuation mark


for(i = 0; i < strlen(str); i++){
if(str[i] == '!' || str[i] == ',' || str[i] == ';' || str[i] == '.' || str[i]
== '?' ||
str[i] == '-' || str[i] == '\'' || str[i] == '\"' || str[i] == ':') {
countPuncMarks++;
}
}
printf("Total number of punctuation characters exists in string : %d
",countPuncMarks);
return 0;
}
Program to determine whether two strings are the anagram
Algorithm

1. Define two strings.


2. Check for their lengths. If the lengths are not equal, then strings
are not an anagram.
3. Else, convert the string to lower case character to make the
comparison easy.
4. Some language allows the strings to provide inbuilt function for
sorting of string. If not then convert them to character array for
sorting.
5. Sort the array.
6. Finally, check for the equality of content.

Input:
Two Strings are called the anagram if they contain the same characters.
However, the order or sequence of the characters can be different.
#include <stdio.h>
#include <string.h>
void toLowercase(char[]);
void sortArray(char[]);
int main ()
{
char str1[] = "Grab", str2[] = "Bragh";
int i, j = 0;

//Checking for the length of strings


if(strlen(str1) != strlen(str2)){
printf("Both the strings are not anagram");
return 0;
}
else {
//converting the string to lowercase
toLowercase(str1);
toLowercase(str2);

//Sorting the arrays by making call to user defined function sortArray()


sortArray(str1);
sortArray(str2);

for(i = 0; i < strlen(str1); i++){


if(str1[i] != str2[i]){
printf("Both the strings are not anagram");
return 0;
}
}
printf("Both the strings are anagram");
}
return 0;
}
void toLowercase(char a[]){
int i;
for(i = 0; i < strlen(a)-1; i++){
a[i] = a[i]+32;
}
}
void sortArray(char a[]){
int temp = 0,i,j;
for(i = 0; i < strlen(a)-1; i++){
for (j = i+1; j < strlen(a); j++){
if(a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}

You might also like