Below is my code for this algorithm:
Code:
#include <stdio.h>
#include <conio.h>
int main()
{
int i, n, totalA;
char studName[31]; // limit the name to 30 characters
// Gets the Student Name into variable studName
printf("Please Input Student Name : ");
gets(studName);
/*
scanf("%s",studName) won't give the correct strlen if a user input such
as "Ahmad Albab"
*/
// find the length of the student's name
i= strlen(studName);
// Start processing the words
totalA=0;
for (n = 0; n <= i; n+=1)
{
if (studName[n]=='A' || studName[n]=='a')
{
totalA++;
}
}
printf("\nTotal number of A or a in the student name is %d",totalA);
getch();
}
The output :
Please Input Student Name : Ahmad Albab
Total number of A or a in the student name is 4
Anybody please explain why if i use scanf, strlen for names with space in the middle is not returning the correct value.