تعداد کلمات یک رشته و یک فایل - هفت خط کد انجمن پرسش و پاسخ برنامه نویسی

تعداد کلمات یک رشته و یک فایل

+1 امتیاز
سلام دوستان

میخوام یک تابع بنویسم که یک رشته دریافت کنه و تعداد کلمات ان را چاپ کنه

برای فایل ها هم میخوام یک برنامه بنویسم که فایل را بخونه و تمام کاراکتر های ان ، کاراکتر های عددی ، کاراکتر های حرفی و تعداد کلمات را چاپ کنه ...

میشه لطفا کمکم کنین ...

ممنونم.
سوال شده آذر 30, 1392  بوسیله ی Azar (امتیاز 628)   29 43 61
دوباره تگ گذاری شد بهمن 22, 1392 بوسیله ی BlueBlade

2 پاسخ

+2 امتیاز
 
بهترین پاسخ

این برای فایل

#include<iostream>
#include<stdio.h>
using namespace std;

int main()
{
	FILE *k;
	char c , c0=EOF,target = 'e';
	k=fopen("E:\\mah.txt","r");
	c=getc(k);

	int lines = 0;
	int words = 0;
	int targets = 0;
	int numbers = 0;
	int chars = 0 ;

	while(c!=EOF)
	{
		if(c != ' '  && c != '\n')
		{
			if(lines == 0)
			{
				lines++;
			}

			if(words == 0)
			{
				words++;
			}

			if(c == target)
			{
				targets++;
			}
			if(c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9' )
			{
				numbers++;
			}
			else if(c != ' ' || c != '\n')
			{ 
				chars++;
			}
			c = getc(k);
		}
		else if(c == '\n')
		{
			while(c == '\n')
			{
				c = getc(k);
			}

			if((c != ' ' && c != EOF))
			{
				lines++;
				words++;
			}
			else if(c == EOF)
			{
				lines++;
			}
		}
		else if(c==' ')
		{
			while(c == ' ')
			{
				c = getc(k);
			}

			if((c != '\n' && c != EOF))
			{
				words++;
			}
		}
	}

	cout << "lines = " << lines << endl;
	cout << "words = " << words << endl;
	cout << "targets = " << targets << endl;
	cout << "numbers = " << numbers << endl;
	cout << "chars = " << chars << endl;

	fclose(k);
	return 0;
}

 

پاسخ داده شده دی 2, 1392 بوسیله ی Azar (امتیاز 628)   29 43 61
انتخاب شد دی 19, 1392 بوسیله ی BlueBlade
+2 امتیاز

این برای رشته

#include <string>
#include <iostream>
using namespace std;

int tedad(string s)
{
	int count = 0;
	for(int i = 0; i < s.length(); i++)
	{
		if(s[i] == ' ') 
		{
			count++;
		}
	}
	return count;
}

int main()
{
	string s;
	char c;
	cout << "matne morede nazar ra vared konid   : ";
	getline(cin, s);
	int natije = tedad(s);
	cout << natije+1 <<endl;
	return 0;
}

 

پاسخ داده شده دی 2, 1392 بوسیله ی Azar (امتیاز 628)   29 43 61
...