Here is an example of how you can do this efficiently if you are allowed to write to a long string:
#include <stdio.h>
If you are not allowed to write to a long line, then you will need to create a temporary line for each line so that the NUL line is completed. Something like that:
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char ** argv) { const char longString[] = "This is a long string.\nIt has multiple lines of text in it.\nWe want to examine each of these lines separately.\nSo we will do that."; const char * curLine = longString; while(curLine) { const char * nextLine = strchr(curLine, '\n'); int curLineLen = nextLine ? (nextLine-curLine) : strlen(curLine); char * tempStr = (char *) malloc(curLineLen+1); if (tempStr) { memcpy(tempStr, curLine, curLineLen); tempStr[curLineLen] = '\0'; // NUL-terminate! printf("tempStr=[%s]\n", tempStr); free(tempStr); } else printf("malloc() failed!?\n"); curLine = nextLine ? (nextLine+1) : NULL; } return 0; }
source share