I'm studying for loops now and I'm trying to incorporate a bodiless for loop that will give me the logical size of a character array. The array's physical size would be 80.
cout<<"Enter your name: "
cin>>name;
Ostensibly, "name" will have white space characters which I do not want to count. I can't figure out if this would be done by counting the entire array and subtracting the white space characters or only counting the printable characters.
Any hints would be appreciated. I have a feeling this is much simpler than I'm making it.
I think the trick would be to loop a counter while evaluating for the '\0' character. Since that marks the end of a c-string, it's index would be the same as the logical length.
#include<iostream.h>
main()
{
char string[80] = "I am browni3141";
int i, count = 0;
for (i = 0; string[i]!='\0'; i++)
cout << string[i];
cout << "\nThe null terminator was found at " << i << ".\n";
for (i = 0; string[i]!='\0'; i++)
if (string[i] != ' ')
count++;
cout << "The string contained " << count << " nonspace characters.\n";
system("pause");
return 0;
}
Lines 7-9 demonstrate what Tabian said. The rest of the code demonstrates what shredded gave with an example string.