Size of string in character array

Posted on 2006-04-28
Last Modified: 2010-04-15
Hi Experts

I have a character array declared as:

char line[2000]

Into this array I put a string which is read from a file.

I need to know if the number of characters in the variable "line" is 12.
I could do it by looping through the array and counting characters until I got to the null pointer.

But is there a better way? Is there something comparable to the LENGTH(STRING) function found in some other languages?

Question by:stummj
    LVL 45

    Accepted Solution

    Hi stummj,

    sure.  Use strlen()

    int   Length;

       Length = strlen ("any string or string array");

    Good Luck!
    LVL 53

    Expert Comment

    And for your reference :

    Don't forget to include <string.h> for this
    LVL 84

    Expert Comment

        #include <string.h>

         strlen(const char *s);

    Author Comment

    Thanks Kent -- I knew there would be an easier way!

    Im trying this...

    if (strlen(line) == 12) do something
    else do something else

    but Im getting an error:

    Operation between types "char*" and "int" is not allowed.

    Help me out?
    LVL 45

    Expert Comment

    Hi stummj,

    hhmmm......    strlen() returns an int, so that's fine.

    Are you sure that the error is on this line?  Is *line* define as "char line[]" or "char *line"?


    Author Comment

    Thanks Kent - the error was on that line but in the "do something" part, not the strlen part. And thanks for that useful link too - good to have in the armoury.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now