需要输入文字而不知道如何 [英] Need to input text and don`t know how

查看:55
本文介绍了需要输入文字而不知道如何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须输入文字,例如

I have to input text, like

Hello,
world!



全部用控制台写的文字,任意数量的字符串。

然后我必须打印它的长度。

也许,这是一个简单的任务,但我没有想法怎么做。

这是我的代码:


All text written in console, any number of strings.
Then I have to print it`s length.
Maybe, it`s an easy task, but I`ve no idea how to do it.
This is my code:

#include <cstdio>
#include <vector>

using namespace std;

int main()
{
	FILE *f = fopen("input.txt", "r");
	char a;
	vector <char> v;
	while (f != NULL && fscanf(f, "%c", &a) != EOF)
		v.push_back(a);
	fclose(f);
	//
	f = fopen("output.txt", "w");
	fprintf(f, "%u", v.size());
	return 0;
}





我的尝试:



在网络中查找相同的问题;

尝试使用字符串,字符类型;

尝试使用cin / cout而不是scanf / printf。



What I have tried:

Looked up for same problems in the Net;
tried using string, char types;
tried using cin/cout instead of scanf/printf.

推荐答案

在阅读每个字符串时,您可以使用 strlen,wcslen,_mbslen,_mbslen_l,_mbstrlen,_mbstrlen_l [ ^ ]找到它的长度。
As you read each string you can use strlen, wcslen, _mbslen, _mbslen_l, _mbstrlen, _mbstrlen_l[^] to find its length.


创建一个名为totalLength的变量。将它初始化为零。

在循环中,将读取的字符串长度添加到totalLength。

循环结束时,打印totalLength。

由于您使用char []存储数据,请参见此处: C库函数 - strlen() [ ^ ]
Create a variable called totalLength. Initialise it to zero.
Inside your loop, add the length of the string you read to totalLength.
When the loop ends, print totalLength.
Since you are using a char[] to store the data, see here: C library function - strlen()[^]


这篇关于需要输入文字而不知道如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆