Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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;
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading