1

I am trying to loop a char*str use this to find out how many lines:

char *str = "test1\ntest2\ntest3";

int lines = 0;

for(int i = 0 ; i < ?? ; i ++ )
{
    if(str[i] == '\n') {
        lines++;
    }
}

I am not sure what to put at the ??, the question is :

1.I mean do I need to use strlen(str) + 1 ?

2.when the str is "test1\ntest2\ntest3\n",does the code still calculate correct lines?

I am using gcc by the way,thanks

6 Answers 6

7

every literal string ends with \0 which is a null character..It depicts the end of the string

So, You can do this

for(int i = 0 ; str[i]!='\0' ; i ++ )
Sign up to request clarification or add additional context in comments.

1 Comment

Not only literal string constants (why the code formatting there anyway?) but in fact everything C can treat as a “string”.
3

To extend the already-existent good answers: the idiomatic way for looping through a C string is

const char *s = "abc\ndef\nghi\n";
int lines = 0;
int nonempty = 0;
while (*s) {
    nonempty = 1;
    if (*s++ == '\n') lines++;
}

If you don't want to count the last empty line as a separate line, then add

if (nonempty && s[-1] == '\n' && lines > 0) lines--;

after the while loop.

9 Comments

I think the condition at the end should be if (lines && s[-1] == '\n'); otherwise unpredictable for the empty string.
It's still indexing outside of the original string in case it was the empty string.
@Arkku not anymore (depends on lazy evaluation, before you spot that str[-1] is still accessed).
Any particular reason to introduce the additional variable nonempty rather than simply checking for lines > 0 (which implies nonempty) before s[-1]? As for s[-1] still being accessed, are you referring to some compiler-specific behaviour where optimizations might cause it to be accessed if it is guaranteed to not be a problem?
@Arkku 1. no specific reason, 2. it's standard behavior.
|
1

Take the length of the string and iterate through all characters.

const unsigned long length=strlen(str);
for(int i = 0 ; i < length ; i ++ )
{
     if(str[i] == '\n') {
       lines++;
   }
}

1 Comment

loops over the string twice. The str[i] resp. str[i] != '\0' solution is better.
1

The following will deliver the same result regardless if the last character is a newline or not.

char *abc = "test1\ntest2\ntest3";

int lines = 0;

{
    bool lastWasNewline = true;
    char * p = abc;
    for (; *p; ++p) {
        if (lastWasNewline) ++lines;
        lastWasNewline = *p == '\n';
    }
}

1 Comment

Have you compiled this? Declaring two variables of different type in a for statement is new to me. And then this test for strlen as loop condition is really, really not the the way to do it.
0

1.I mean do I need to use strlen(str) + 1 ?

no, just use str[i] for i < ??, this tests if that is the 0 character which terminates the string

2.when the abc is "test1\ntest2\ntest3\n",does the code still calculate correct lines?

no, you code assumes that the input is broken into one input line per buffer line[j].

1 Comment

Don't use str[i] for ??, but use str[i] instead of i < ??.
0

in place of ?? put strlen(abc) and make sure #include <string.h>

For better efficiency do

int length= strlen(abc);

and then use i < length

Or use str[i]!= '\0'

7 Comments

That's not efficient: at every iteration it will calculate again strlen.
@RamyAlZuhouri I doubt OP already cares about efficiency if he can't even enumerate the characters of a string...
@H2CO3 It's still terrible advice to suggest something like this to beginners when the correct solution is no more complex.
@Arkku What if I tell you most decent compilers will optimize that out amyway?
@H2CO3 Umm, "the compiler will optimize it" is a considerably worse excuse to teach bad practices than "it doesn't matter in this case"; the compiler's optimization cannot, in the general case, be relied upon, it's not visible in the code, and it causes problems with different compilers & different optimization flags, etc.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.