Create a free account on Computer Sir Ki Class and unlock access to valuable learning resources.
Exam Paper Downloads: After registering and logging in, you can download exam question papers in PDF format to practice and prepare for your exams.
Student Support: Get assistance with your doubts, queries, and questions related to your studies. Our support is here to help you understand topics and resolve your learning difficulties.
Register today and make your exam preparation easier and more effective!
Check character group as alpha, digit, control, punctuation, lower, upper etc.
#include
using namespace std;
int main()
{
char str[80];
int sp_count=0;
cout<<"Input a small sentence: " ;
cin.getline(str,80);
for(int i=0;str[i]!='';i++)
{
if(str[i]==' ') sp_count++;
}
cout<<"Number of spaces in given input ="
<
×
Ask AI About This Code
Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.
×
Test CPP
The code has been copied automatically for you in the clipboard. After that open any of the service and press cntrl-v to paste in the given code area. Needs a bit of tweaking as per the link.
Related codes are those codes where similar programming concepts are in use. They are different from alternate codes in the sense that they may not have same purpose and results.
:
Run Output
Input a small sentence: Hello how are you?
Number of spaces in given input =3
char str[80]; //Character array for string storage. int sp_count=0; //variable for initialisation of space count
cout<<“Input a small sentence:” ; cin.getline(str,80);
Collecting the user input in given character array and putting the null character automatically.
for(int i=0;str[i]!=”;i++) //The loop runs till null character is found
{ if(str[i]==’ ‘) sp_count++; //The sp_count is increased the moment match of character at given position while iteration is equal to space character. single space character is given in single quotes with one space in between.
} cout<<“Number of spaces in given input =”<<sp_count<<endl;
Output is printed.
Notes
Using this technique any specific character or a range of characters can be found.
The logic of using isspace() check can also be used if we need to count all kinds of white spaces lie space, tab etc.
For loop traversing check with length returned from strlen() function can also be used.