whileループを使用して文の平均単語長を計算する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 the function at step 5 Step 4: Print the average length return 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 it trues enter into loop 1. if(*stmt != ' ') 2. charcount++; 3. else if(*stmt == ' ') 4. wordcount++; 5. 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.
-
リンクリストの長さを見つけるCプログラム
リンクリストは動的メモリ割り当てを使用します。つまり、それに応じて拡大および縮小します。それらはノードのコレクションとして定義されます。ここで、ノードにはデータとリンクの2つの部分があります。データ、リンク、およびリンクリストの表現を以下に示します- リンクリストの種類 リンクリストには次の4種類があります- 単一/単一リンクリスト 二重/二重リンクリスト 循環単一リンクリスト 循環二重リンクリスト 再帰メソッドを使用してリンクリストの長さを見つけるために使用したロジックは-です。 int length(node *temp){ if(temp==NUL
-
Cプログラムで十角形の周囲長を計算するプログラム
デカゴンとは何ですか? サイドで与えられたタスクは、十角形の周囲長を計算することです。十角形は10辺のポリゴンの一種であるため、10角形ポリゴンとも呼ばれます。 10個の頂点とエッジがあります。正十角形の辺の長さは等しく、内角はそれぞれ144度です。 以下はデカゴンの図です 円錐台の体積と表面積を計算するには、次の式があります Perimeter = 10 * Side 例 Input-: side=10 Output-: perimeter of Decagon is : 100 Input -: side = 20 Output -: perimeter of Decagon