How to count the number of lines from a string in C

1 Answer

0 votes
#include <stdio.h>

int main(void) {
    char s[] = "line1\nline2\nline3\nline4\nlin5\nline6";
    int lines = 1;
    
    for (int i = 0; s[i] != '\0'; i++) {
      if (s[i]=='\n') {
         lines++;
      }
    }
    printf("%d", lines);
}



/*
run:

6

*/

 



answered Aug 19, 2021 by avibootz

Related questions

1 answer 205 views
1 answer 203 views
1 answer 204 views
1 answer 154 views
154 views asked Oct 7, 2015 by avibootz
1 answer 148 views
1 answer 190 views
2 answers 198 views
...