Lab 9
Lab 9
Submitted to-
Sobhana Jahan
Lecturer
Department of Computer Science and Engineering
Bangladesh University of Professionals
Mirpur Cantonment, Dhaka-1216
Problem Description:
Write a program to count the number of consonants in a given string. The string may contain any alphabet,
digits and special characters.
Source Code:
#include<stdio.h>
#include<string.h>
int main(void)
char string[100];
int i, count=0;
fgets(string,sizeof(string),stdin);
char c=tolower(string[i]);
count++ ;
} }
Problem Description:
Write a C program and input a integer number, N in decimal form and converts to its binary representation.
Source Code:
#include <stdio.h>
int main(void)
{
int n;
scanf("%d",&n);
int arr[32];
int index=0;
while(n>0)
{
arr[index]=n%2;
n/=2;
index++;
}
}
}
Problem Description:
Write a C program to find the longest word of a given string. You can assume the only delimiter is
space.
#include<stdio.h>
#include<string.h>
int main(void)
{
printf("Say Something:\n");
char str[1000];
fgets(str,1000,stdin);
if(str[strlen(str)-1]=='\n')
{
str[strlen(str)-1]='\0';
}
int len=strlen(str);
int max=-9999;
int previous_word_end=0,letter_count=0;
for(int i=0; i<=len; i++)
{
if(str[i]==' '||str[i]=='\0')
{
if(letter_count==0)
{
letter_count = i;
previous_word_end=i;
}
}
}
if(letter_count>max)
{
max=letter_count;
}
}
printf("%d",max);
}
Output:
Write a program to count the number of tokens in a given string where the delimiters are
Source Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
printf("Say Something:\n");
char str[1000];
fgets(str, 1000, stdin);
if (str[strlen(str) - 1] == '\n') {
str[strlen(str) - 1] = '\0';
}
printf("%d\n", word_count);
Output: