[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Size of string in character array

Posted on 2006-04-28
6
Medium Priority
?
260 Views
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?

Thanks
0
Comment
Question by:stummj
6 Comments
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 2000 total points
ID: 16562467
Hi stummj,

sure.  Use strlen()


int   Length;

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



Good Luck!
Kent
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 16562531
And for your reference :

http://www.cplusplus.com/ref/cstring/strlen.html

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

Expert Comment

by:ozo
ID: 16562569
    #include <string.h>

     size_t
     strlen(const char *s);
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:stummj
ID: 16562596
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?
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 16562645
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"?


Kent
0
 

Author Comment

by:stummj
ID: 16562709
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.
Julian
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

825 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