Home > OS >  Why does my code show nothing in the program after i press run?
Why does my code show nothing in the program after i press run?

Time:06-12

I am a beginner in c and I want to make a program that will show me all of the small letters in a sentence. But somehow it doesn't work. Can you help me out? Maybe something is wrong? Code :

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    char text[] = "i went to the beach yesterday.";
    //How many small letters are here?
    int small_letters = 0;
    
    for(int i=0; strlen(text); i  )
    {
        if(text[i] >= atoi("a") && text[i] <= atoi("z"))
        {
            small_letters  ;
        }
    }
    
    cout<<small_letters;
}

CodePudding user response:

So a few errors

for(int i=0; strlen(text); i  )

should be

for(int i=0; i<strlen(text); i  )

You forgot to compare i with the length of the string.

if(text[i] >= atoi("a") && text[i] <= atoi("z"))

should be

if(text[i] >= 'a' && text[i] <= 'z')

I guess you are confused about the difference between a character like 'a' and a string like "a". In C these are not the same. A string is a sequence of characters. If you are dealing with characters then use single quotes.

  •  Tags:  
  • c
  • Related