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 195 views
1 answer 197 views
1 answer 197 views
1 answer 149 views
149 views asked Oct 7, 2015 by avibootz
1 answer 142 views
1 answer 185 views
2 answers 191 views
...