কম্পিউটার টিউটোরিয়াল

C প্রোগ্রাম: হয়াইল লুপ ব্যবহার করে সেন্টেন্সের গড় ওয়ার্ড লেথ ক্যালকুলেট করা


সমস্যা

রানটাইমে একটি সেন্টেন্স ইনপুট নিন এবং সেন্টেন্স-এ থাকা ওয়ার্ডগুলোর গড় লেথ ক্যালকুলেট করার কোড লিখুন।

সমাধান

অ্যালগরিদম

START
Step 1: declare character, int and double variables
Step 2: Enter any statement
Step 3: while loop
         Check condition stmt[i] = getchar()) != '\n'
         True then enter into loop
         Increment i and call function at step 5
Step 4: Print the average length returned by function from step 5
Step 5: called function calculatewordlength
         i. declare and initialize charcount = 0 and wordcount = 1
         ii. while loop
              check condition (*stmt != '\n')
              if true enter into loop
              1. if (*stmt != ' ') charcount++;
              2. else if (*stmt == ' ') wordcount++;
              3. stmt++;
         iii. return (double)charcount / wordcount;
STOP

প্রোগ্রাম

#include<stdio.h>
#include<string.h>
double calculatewordlength(const char *stmt);
int main() {
    char stmt[100];
    int i = 0;
    double avglen;
    printf("enter any statement:");
    while ((stmt[i] = getchar()) != '\n')
        i++;
    stmt[i] = '\n';
    avglen = calculatewordlength(stmt);
    printf("average length of word is:%f.\n", avglen);
}
double calculatewordlength(const char *stmt) {
    int charcount = 0;
    int wordcount = 1;
    while (*stmt != '\n') {
        if (*stmt != ' ')
            charcount++;
        else if (*stmt == ' ')
            wordcount++;
        stmt++;
    }
    return (double)charcount / wordcount;
}

আউটপুট

enter any statement: Tutorials Point is the best resource for online education
average length of word: 5.444444444.

  1. ফর লুপ দিয়ে ডিজিটকে শব্দে রূপান্তরের C প্রোগ্রাম

  2. ডাইনামিক লিংকড লিস্ট ব্যবহার করে কার ইনফরমেশন স্টোর করার সি প্রোগ্রাম

  3. লিংকড লিস্টের দৈর্ঘ্য বের করার C প্রোগ্রাম

  4. C প্রোগ্রাম: ডেকাগন (Decagon) এর পরিসীমা গণনা