C++,输入一组字符串,把字符串中每个字符的ascii码相加,输出和

如题所述

#include<iostream>
using namespace std;
int  main(){
char c[50];//定义字符串存储空间
cin>>c;
int sum = 0;
for(int i = 0; c[i] != '\0'; i++){
 sum += c[i];
}
cout<<"sum = "<<sum<<endl;
return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-03-01
//#include "stdafx.h"//If the vc++6.0, with this line.
#include <iostream>
using namespace std;
int main(void){
    char str[1000];
    cout << "Enter a string...\n";
    cin >> str;
    for(int sum=0,i=0;str[i];sum+=str[i++]);
    cout << "The total is " << sum << endl;
    return 0;
}

本回答被网友采纳
相似回答