如何将字符串转换为 ASCII 值的总和
How to convert a string into the sum of ASCII values?
我是初学者,正在参加CSC课程,我必须编写一个程序,将用户输入字符串转换为每个字符的ASCII值的总和,这是我到目前为止所拥有的,我离完成还很远。但任何帮助将不胜感激。谢谢
#include <iostream>
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
using std::string;
using std::cout;
using std::endl;
int main()
{
{
int x;
std::cout << "enter string" << std::endl;
std::cin >> x;
}
string text = "STRING";
for (int i = 0; i < text.size(); i++)
cout << (int)text[i] << endl;
return 0;
}
您可以使用基于范围的for
循环遍历字符串,然后在以下内容中将每个char
相加:
#include <iostream>
#include <string>
int main()
{
int sum = 0; // this is where all the values are being added to
std::string s;
std::cout << "enter string and press enter." << std::endl;
std::cin >> s; // string that the user enters will be stored in s
for (char c : s)
sum += c;
std::cout << "total ASCII values: " << sum << std::endl;
return 0;
}
相关文章:
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- 如何找到串联ASCII和std::string的子字符串
- 在 C++ 中从 8 位 ASCII 字符创建 7 位 ASCII 文本字符串
- 编写具有非ASCII数据的std ::字符串
- URL 编码扩展的 ASCII 标准::字符串在 C++.
- c 字符串开始时ASCII值是什么
- 如何与C 字符串中的非ASCII字符一起工作
- 短(ASCII,每个字符 7 位)字符串存储和C++中的比较优化
- 尝试显示 ASCII 字符串,但它被截断
- 如何将二进制字符串转换为 ASCII 字符串
- Win32中的任何易于使用的哈希功能都可以放置ASCII字符串
- 如何强制用户/操作系统输入 Ascii 字符串
- 如何使用 Windows API WriteFile 将 ASCII 字符串写入文件
- 将BITSet转换为ASCII字符串
- 将 ASCII 字符串转换为 UTF-16,然后再将它们传递给 Windows API 函数
- 将无符号长字符串转换为ascii字符串
- 将IPv6地址转换为Internet标准点分十进制格式的ASCII字符串
- 将ASCII字符串转换为long long
- 如何将十六进制转换为ASCII字符串
- 将二进制字符串转换为ASCII字符串(C++)