0

Addmitedly and obviously I am new to this coding thing. I'm enjoying working it out but I feel stuck on this problem. Any guidance would be appreciated!

I am attempting to count all of the letters of a particular text from the user, but my counter (i) comes out as 1 when I run the program regardless of input. Below is my code.

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>



int main(void)
{

    string a = get_string("Text: \n"); // Get input


    for (int i = 0, n = strlen(a); i < 1; i++) //Set counter for number of letters
        {
        if isalpha(a[i]). // Count only if character is a letter
            {i++;}
         printf("%i\n", i); // print counter
        }

}

Again, any guidance (in as simple of terms as possible!) is appreciated as I've been trying to figure this out for two days.

  • 1. You are using `i` both as the counter and the loop iterator. 2. Your `for` loop only looks at the first character as the condition is `i<1`. – kaylum Apr 28 '20 at 00:52
  • C does not have a type `string`. Is this a special CS50 type? Are you using C++? – abelenky Apr 28 '20 at 01:20
  • You already asked this 3 days ago, and got some very good answers then. https://stackoverflow.com/questions/61399924/trouble-implementing-isalpha/ – abelenky Apr 28 '20 at 01:23
  • Thanks for the help kaylum and abelenky. Abelenky, I did ask a similar question but couldn't get it right based on those answers. Again, probably more on my understanding than the advice not being sound. – Dizzy_Parking Apr 29 '20 at 00:51

2 Answers2

0

in your for loop, the i < 1 part means you will loop while i is smaller than 1, it is initialised to 0 on the first loop, so it runs (and counts to 1), and then at the end of the loop, i increments to 1, and therefore the loop will not run again. You need to put i < n instead of i < 1. and you also need to change the counter to j (or another variable), as mentioned in the comments.

Liam Webb
  • 1
  • 3
0

So i've edited your code a bit to where it would make sense and I'll try to explain why I changed some stuff. I'm a bit rusty with c/c++ so I'd appreciate if anyone can point out what I'm wrong with.

so here's a snippet of code that would count the number of letters.

#include <stdio.h>
#include <string.h>
#include <ctype.h>



int main(void)
{

    char a[] = "test";
    int n = strlen(a);
    int i = 0;


    for (i = 0; i < n; i++) //Set counter for number of letters
        {

        }
        printf("%i\n", i); // print counter

}

So with your code a couple things.

(1) for (int i = 0, n = strlen(a); i < 1; i++)

A for loop looks like this

for(where you start your loop at ; where you stop; count up or down)

so with your code I did this:

for (i = 0; i < n; i++) //Set counter for number of letters

where we start i with 0, we go till i hits n and we count up

now you can do this if it makes more sense:

for (i = 0; i < n; i++) //Set counter for number of letters
        {
            i++;
        }

but they'll both do the same thing (I think) since we are iterating i, but I do think this is better due to readability.

(2) printf("%i\n", i); // print counter

As you probably know this prints out the number of letters and you had this in your for loop and that's fine when you're doing something like a countdown. So if we put it in the for loop it would look like this:

for (i = 0; i < n; i++) //Set counter for number of letters
        {
          printf("%i\n", i); // print counter
        }

where it would print out 0 1 2 3 (Remember arrays always start at 0), so by keeping it outside of the for loop, you just have a counter for how many times 'i' was iterated in the for loop, so you get 4.

chrisHG
  • 80
  • 1
  • 2
  • 18
  • Thanks for giving this a look- I appreciate the different way to look at the for loops. That definitely helps moving forward. I think the issue with your code is that I have to ask a user for input, so their input could be sentences or paragraphs long and I still go back and count letters. That is where I think the strlen and isalpha come into play here. If I just run your code, it would be on a predetermined word. Hope that makes sense and again I appreciate you looking at it. – Dizzy_Parking Apr 29 '20 at 00:42
  • I was just able to implement what you were talking about and got it to work! Thanks a ton- as an above commenter mentioned, I have been stuck on this for days. Now I got it working and understand where I was going wrong. Thanks a ton! – Dizzy_Parking Apr 29 '20 at 00:50