I am trying to write a code to get the number of letters in a text as a variable. I used a for loop to check each character in the text to see whether it is a letter or not. The problem is, I can't get the 'letters' variable out for further use in the continuation of the code since it is in a for loop. Is there a better way to do it?
#include<stdio.h>
#include<cs50.h>
#include<string.h>
int non_letter = 0;
int main(void)
{
string text = get_string("Text:\n"); //get 'text' from user
int size = strlen(text); //get length of 'text' string
for (int x = 0; x < size ; x++)
{
if (text[x] < 65 || text[x] > 122) //get the number of non letter in 'text'
{
non_letter = non_letter + 1;
}
int letters = x - non_letter + 1;
}
}