0% found this document useful (0 votes)
41 views

Exercise Programs (Unit 3)

The document contains 3 C programs with their outputs. The first program counts the frequency of a character in a string. The second program counts the numbers of vowels, consonants, digits, spaces, words and characters in an input text. The third program sorts student names alphabetically using string comparison and copying functions.

Uploaded by

Praveena Gopi
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Exercise Programs (Unit 3)

The document contains 3 C programs with their outputs. The first program counts the frequency of a character in a string. The second program counts the numbers of vowels, consonants, digits, spaces, words and characters in an input text. The third program sorts student names alphabetically using string comparison and copying functions.

Uploaded by

Praveena Gopi
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

Exercise programs

1.To find the frequency of a character in a string

#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;
}

OUTPUT

Enter a string: This website is awesome.


Enter a character to find its frequency: e
Frequency of e = 4
2.To find the number of vowels, consonants and white spaces in a
given text
#include<stdio.h>
    int main(){
                  char words[200];
                  int vowels=0, letters=0, word=0, digits=0, spaces=0;
                  int flag=0, i;
                  clrscr();
                  printf("Enter a line of Text :\n");
                  gets(words);
                  for(i=0;words[i]!='\0';++i)
                 {
                 if(words[i]=='a' || words[i]=='e' || words[i]=='i' || words[i]=='o' ||  
words[i]=='u' || words[i]=='A' || words[i]=='E' || words[i]=='I' || words[i]=='O' ||
words[i]=='U')
                 ++vowels;
                 else
                 if((words[i]>='a'&& words[i]<='z') || (words[i]>='A'&& words[i]<='Z'))
                 ++letters;
                 else
                 if(words[i] >='0' && words[i] <='9')
                 ++digits;
                 else
                 if (words[i]==' '){
                 ++spaces;
                 flag=0;}
                 if (words[i] !=' '&& flag==0){
                 ++word;
                 flag=1;
                 }
                 }
                 letters += vowels;
                 printf("\n\n Number of words:  %d", word);
                 printf("\n Number of Digits:  %d", digits);
                 printf("\n Number of Consonants:  %d", letters); 
  //Alphabetic letters 
                 printf("\n Number of Vowels:  %d", vowels);
                 printf("\n Number of White spaces:  %d", spaces);
                 printf("\n Number of Characters:  %d", i);
                 getch();
    return 0;
  }
OUTPUT:
Enter a line of Text :
I am '30' years old
Number of words:   5
Number of Digits:   2
Number of Consonants:   11
Number of Vowels:   5
Number of White spaces:   4
Number of Characters:   19

3. Program for C program sorting the names.

#include <stdio.h>
#include <conio.h>
#include<string.h>
int main()
{
char names[5][10], temp[10];
int i, n, j;
clrscr();
printf("\n Enter the number of students : ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n Enter the name of student %d : ", i+1);
gets(names[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if(strcmp(names[j], names[j+1])>0)
{
strcpy(temp, names[j]);
strcpy(names[j], names[j+1]);
strcpy(names[j+1], temp);
}
}
}
printf("\n Names of the students in alphabetical order are : ");

for(i=0;i<n;i++)

puts(names[i]);
getch();

return 0;

OUTPUT
Enter the number of students : 3

Enter the name of student 1 : Goransh

Enter the name of student 2 : Aditya

Enter the name of student 3 : Sarthak

Names of the students in alphabetical order are : Aditya Goransh Sarthak

You might also like