Could you give an example of what it is you want to count. Do you just mean the size, i.e. total number of characters, something else, perhaps the number of occurrences of a particular character, or how many digits ....
Well i get a string as an input that string could be ,i.e.
string s = "hy150 = 27 10 66 70 67";
So how can i count these elements that i got in my string ?
Then you could add whatever other code you needed, e,g. to count them add 1 to a counter inside the loop. Or you could store them in an array or vector, whatever is needed.
Of course you could use other more hands-on methods, such as inspecting each character until you find a space, then inspect until you find a non-space and so on, do the parsing yourself. Or use the string find() function or one of its variations.
I want to copy all the elements afte the '=' to an another array without the whitespaces and if i run the programm says segmentation fault why is that?
int main ()
{
int k=0,word_l,position,num_dec[10],i,j;
char word[21];
cin.getline(word,21); // my input is "hy150 = 27 10 66 70 67"
while (word[word_l++] != '\0');
for(i=0;i<word_l;i++)
{
if(word[i]== '=')
{
position=i;
for(j=++i;j<word_l;k++,j++)
{
num_dec[k]=word[j];
}
}
}
for(i=0;i<k;i++)
cout << num_dec[i];
return 0;
}
The input "hy150 = 27 10 66 70 67" requires an array size of at least 23 characters to store, including the null terminator. (C++ std::string is more convenient as it adjust in size as needed).
the programm says segmentation fault why is that?
What is the initial value of word_l ?
Also, array num_dec[10] may not be large enough, this loop would seem to overrun beyond the array boundary.