I am trying to write a function that counts the number of characters in a string excluding spaces. However, the output is always wrong so there is something wrong with my code.
There is something wrong with my code but I can't figure it out. Please help me with my programming homework.
#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
#include <ctype.h>
using namespace std;
int countLetters (char s[], int size_s){
int isLetter = 0;
for(int i =0; i<size_s;i++){
if (isalpha(s[i])){
isLetter ++;
}
}
return isLetter;
}
int main (){
char s[100];
gets(s);
int n = sizeof(s)/sizeof(s[0]);
cout << countLetters(s,n);
}
Here is an example of the wrong output:
hi
10
PS C:\Users\user\OneDrive\Desktop\cpp practical> cd "c:\Users\user\OneDrive\Desktop\cpp practical\" ; if ($?)
{ g++ count_letters.cpp -o count_letters } ; if ($?) { .\count_letters }
hi
6
PS C:\Users\user\OneDrive\Desktop\cpp practical> cd "c:\Users\user\OneDrive\Desktop\cpp practical\" ; if ($?)
{ g++ count_letters.cpp -o count_letters } ; if ($?) { .\count_letters }
hi
10
PS C:\Users\user\OneDrive\Desktop\cpp practical>