#include <iostream>
#include <string.h>
usingnamespace std;
int main(){
char palabra[250];
cin.getline(palabra, 250);
cout<<endl;
int y = strlen(palabra);
cout<<endl;
cout<<"Tamano total de la frase -> "<<y<<endl;
cout<<"*********************************"<<endl;
int i = 0;
int w = 0;
while(i < y){
if(int(palabra[i]==32)){
w++;}
i++;
}
cout<<"El numero de espacios dentro de la frase es: "<<w<<endl;
cout<<"El tamaño del nuevo arreglo es: "<<y-w<<endl;
cout<<"*********************************"<<endl;
cout<<endl;
cout<<endl;
int z = y-w;
cout<<"z = "<<z<<endl;
char palabra2[z];
i = 0;
int j = 0;
for (int i = 0, j = 0; i<y; i++,j++){
if (palabra[i]!=' ')
palabra2[j] = palabra[i];
else
j--;
}
cout<<"La nueva palabra sin espacios es: "<<palabra2<<endl;
int sum;
for (int i = 0; i <= z; i++){
sum = int(palabra2[i]);
}
cout<<"El peso de la palabra es: "<<sum<<endl;
}
sum is supposed to be: int(palabra2[0])+int(palabra2[1]).....
Thanks kevinkjt2000 for your reply :), that worked well.
Now, I can´t sum all those values into a one single variable. Any ideas?
1 2 3 4 5 6 7 8 9 10 11 12 13
int sum = 0;
for (int i = 0; i <= z; i++){
if(palabra2[i] >= 97 && palabra2[i] <= 122){
sum = int(palabra2[i]);
}
cout<<"El peso de la palabra es: "<<sum<<endl;
}
}
palabra2[0]+palabra2[1]+palabra2[2]+palabra2[3]+...
h+e+l+l+o...
104+101+108+108+111...
If that is what you want, then do what I suggested earlier to line 55 or line 6 on your second post.