#include <stdio.h>
#include <string.h>
#include <cs50.h>
int main(void)
{
string enterword = get_string("input");
int t = strlen(enterword);
printf("%i\n", t);
for(int i = 0; i<= t; i++)
{
int counter = 0;
while (enterword[i] == '.')
{
counter = counter + 1;
}
printf("counter is %i",counter);
}
}
The above is my code which is intended to count no. of full stops but fails. The program compiles successfully with counting no. of characters.
Revised:
#include <stdio.h>
#include <string.h>
#include <cs50.h>
int main(void)
{
string enterword = get_string("input");
int t = strlen(enterword);
printf("%i\n", t);
for(int i = 0; i<= t; i++)
{
int counter = 0;
if (enterword[i] == '.') {
counter = counter + 1;}
printf("counter is %i",counter);
}
}
What is intended is with counter, if full stop recorded, counter = counter + 1. But there is definitely fault in my coding. Maybe, due to placement of counter variable in the wrong place, leading to issues with local and global variable.