0

I started working on readability and I am trying to figure out how to count the letters. I found a way but it includes all the characters so now I am trying to eliminate all the entries besides the alphabets. So to do that I wrote the following code-

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

int main(int argc, string argv [])
{
    int i, words, sentences, n;
    string s = get_string("String: ");
    n = strlen(s);
   
   for (i = 0; s[i] != 0 || s[i] != 1  ;i ++)
   {
      ;
   }
    
   printf ("%d", i);
    
}

Right now in this stage I believe it should count all the entries except the digits 0 and 1 however if I input anything like say for instance 101 or 12302 or even just "Hey Man" I get a Segmentation fault error. I suspect it's probably due to the use of the or operator in the for loop but I can't figure out why? It was working perfectly fine until now when I had only 1 not equals operation in the for loop.

1

the issue is the condition you wrote inside the for loop for i (s[i] != 0 || s[i] != 1 ). you need to set a limit for i (when will the for loop terminate ? = when will the i stop increasing by 1?). otherwise i increases forever and you run out of memory , thus your seg.fault.

have another look at the different types of loops (for vs if vs while) - the short videos from week 1 are very helpful.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .