0% found this document useful (0 votes)
19 views9 pages

Lab 9

Uploaded by

alajoshim
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)
19 views9 pages

Lab 9

Uploaded by

alajoshim
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/ 9

Lab Number# 09

Course title: Structured Programming Language Laboratory


Course code: CSE-1204
Session: July-December 2024

Date of Submission: 6/1/2025

Submitted to-

Md. Rafsan Jani


Assistant Professor
Department of Computer Science and Engineering
Jahangirnagar University
Savar, Dhaka-1342

Sobhana Jahan
Lecturer
Department of Computer Science and Engineering
Bangladesh University of Professionals
Mirpur Cantonment, Dhaka-1216

Sl Class Roll Name

01 24524203145 Md.Zafor Iqbal Sakil

Department of Computer Science and Engineering


Bangladesh University of Professionals
Mirpur Cantonment, Dhaka-1216
Problem Number: 01

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;

printf("Enter the Word :");

fgets(string,sizeof(string),stdin);

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

char c=tolower(string[i]);

if((c>='a'&&c<='z')&& c!='a'&& c!='e'&& c!='i'&& c!='o'&& c!='u')

count++ ;

} }

printf("Number of consonant :%d",count); }

Department of Computer Science and Engineering


Bangladesh University of Professionals
Mirpur Cantonment, Dhaka-1216
Page 2 of 9
Output:

Department of Computer Science and Engineering


Bangladesh University of Professionals
Mirpur Cantonment, Dhaka-1216
Page 3 of 9
Problem Number: 02

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

for (int i=index-1; i>=0; i--)


{
printf("%d",arr[i]);

}
}

Department of Computer Science and Engineering


Bangladesh University of Professionals
Mirpur Cantonment, Dhaka-1216
Page 4 of 9
Output:

Problem Description:

Write a C program to find the longest word of a given string. You can assume the only delimiter is
space.

Department of Computer Science and Engineering


Bangladesh University of Professionals
Mirpur Cantonment, Dhaka-1216
Page 5 of 9
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';
}
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;
}

Department of Computer Science and Engineering


Bangladesh University of Professionals
Mirpur Cantonment, Dhaka-1216
Page 6 of 9
else{
letter_count = i - previous_word_end-1;
previous_word_end=i;

}
}
if(letter_count>max)
{
max=letter_count;
}
}
printf("%d",max);
}

Output:

Department of Computer Science and Engineering


Bangladesh University of Professionals
Mirpur Cantonment, Dhaka-1216
Page 7 of 9
Problem Description:

Write a program to count the number of tokens in a given string where the delimiters are

(space, multiple space, * and #) .

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

int len = strlen(str);


int word_count = 0;
int in_word = 0;

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


{

Department of Computer Science and Engineering


Bangladesh University of Professionals
Mirpur Cantonment, Dhaka-1216
Page 8 of 9
if (str[i] == ' ' || str[i] == '*' || str[i] == '#' || str[i] == '\0')
{
if (in_word)
{
word_count++;
in_word = 0;
}
}
else
{
in_word = 1;
}
}

printf("%d\n", word_count);

Output:

Department of Computer Science and Engineering


Bangladesh University of Professionals
Mirpur Cantonment, Dhaka-1216
Page 9 of 9

You might also like