সমস্যা
কনসোলে ব্যবহারকারীর দেওয়া একটি অক্ষর কতবার একটি বাক্যে রিপিট হয়েছে তা strlen() ফাংশন ব্যবহার করে স্ক্রিনে প্রিন্ট করার প্রোগ্রাম লিখুন।
সমাধান
অক্ষর গণনা করার লজিক নিম্নরূপ:
- ব্যবহারকারীকে রানটাইমে একটি বাক্য (sentence) ইনপুট দিতে বলুন।
printf("Enter a sentence\n"); gets(str);
- ব্যবহারকারীকে রানটাইমে একটি অক্ষর (letter) ইনপুট দিতে বলুন।
printf("Enter a character to check how many times it is repeating\n"); scanf("%c",&c);
- বাক্যে অক্ষর গণনা করার লজিক নিম্নরূপ:
for(i=0;i<strlen(str);i++){ if(str[i]==c){ count++; } }
- শেষে কাউন্ট প্রিন্ট করুন।
উদাহরণ
বাক্যে একটি অক্ষর কতবার রিপিট হয়েছে তা গণনা করার C প্রোগ্রাম:
#include<stdio.h> #include<string.h> main(){ int i,count=0; char c,str[100]; printf("Enter a sentence\n"); gets(str); printf("Enter a character to check how many times it is repeating\n"); scanf("%c",&c); for(i=0;i<strlen(str);i++){ if(str[i]==c){ count++; } } printf("Letter %c repeated %d times\n",c,count); }
আউটপুট
উপরোক্ত প্রোগ্রামটি এক্সিকিউট করলে নিচের আউটপুটটি দেখায়:
Enter a sentence Here are the C Programming question and answers Enter a character to check how many times it is repeating n Letter n repeated 4 times