// 读取字符串并计算总字符数.cpp
/*******************************************************************
程序的输出:
Enter a line:
nice pants
"nice pants" contains 9 characters
9 characters total
Enter next line(empty line to quit):
thanks
"thanks"contains 6 characters
15 characters total
Enter next line(empty line to quit):
parting is such sweet sorrow
"parting i" contains 9 characters
24 characters total
Enter next line(empty line to quit):
ok
"ok" contains 2 charsacters
26 characters total
Enter next line (empty line to quit):
Bye
*******************************************************************/
#include <iostream>
using namespace std;
inline int countTotal(const char* t, int n)
{
int i = 0;
while(t[i] != '\0')
{
i++;
++n;
}
return n;
}
int main()
{
cout << "Enter a line:" << endl;
char temp[10];
int n = 0;
int countTotal(const char*, int n);
cin.getline(temp, 10);
while(temp[0])
{
cin.clear();
n = countTotal(temp, n);
cout << "\"" << temp << "\"" << " contains "
<< sizeof(temp) - 1 << " characters ";
cout << endl << n << " characters total" << endl
<< "Enter next line (empty line to quit):" << endl;
cin.getline(temp, 10);
}
cout << "Bye" << endl;
return 0;
/*******************************************************************
程序的输出:
Enter a line:
nice pants
"nice pants" contains 9 characters
9 characters total
Enter next line(empty line to quit):
thanks
"thanks"contains 6 characters
15 characters total
Enter next line(empty line to quit):
parting is such sweet sorrow
"parting i" contains 9 characters
24 characters total
Enter next line(empty line to quit):
ok
"ok" contains 2 charsacters
26 characters total
Enter next line (empty line to quit):
Bye
*******************************************************************/
#include <iostream>
using namespace std;
inline int countTotal(const char* t, int n)
{
int i = 0;
while(t[i] != '\0')
{
i++;
++n;
}
return n;
}
int main()
{
cout << "Enter a line:" << endl;
char temp[10];
int n = 0;
int countTotal(const char*, int n);
cin.getline(temp, 10);
while(temp[0])
{
cin.clear();
n = countTotal(temp, n);
cout << "\"" << temp << "\"" << " contains "
<< sizeof(temp) - 1 << " characters ";
cout << endl << n << " characters total" << endl
<< "Enter next line (empty line to quit):" << endl;
cin.getline(temp, 10);
}
cout << "Bye" << endl;
return 0;
}
//还是有很多问题,太晚了,先保存起来,明天再作修改。