I have to make a simple function to display the number of whitespaces in a predefined array of characters. This is my code : (please ignore the first part, only last part and whitespaces function)
#include <iostream>
#include <conio.h>
using namespace std;
int whitespaces(char[]);
void main(){
char a[] = "Array of characters for test\n"; //Simple array of characters
cout << "\n This is a test : " << a;
char b[] = "\n\t Another array of character for test\n"; //Simple array with some escape sequences, \t and \n
cout << "\n This is a test for usage of escape sequences : " << b;
char c[] = "T E S T\n"; //Simple array with 8 characters and whitespaces
cout << "\n Last test for whitespaces : "<<c;
int ws = whitespaces(c);
cout << "\n Number of whitespaces for the last array of characters is : "<<ws;
_getch();
}
int whitespaces(char c[]){
int count = 0;
for (int n = 0; n <= sizeof(c); n++) {
if (c[n] == ' '){
count++;
}
}
return(count);
}
Using Visual Studio express