Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int linecount(char * string);
- char ** splitlines(char * string);
- void replacechar(char * string, char to, char with);
- int main(int argc, char ** argv)
- {
- char test[] = "Pierwsza linijka.\n Druga linijka.\n Trzecia linijka. \n";
- char ** lines = splitlines(test);
- while(**lines)
- printf("%s\n", *lines++);
- return 0;
- }
- int linecount(char * string)
- {
- int count = 0;
- while(*string)
- count += (*string++ == '\n');
- return count;
- }
- char ** splitlines(char * string)
- {
- int lcount = linecount(string);
- char ** lines = malloc((lcount + 1) * sizeof(char*));
- int loffset = 0;
- char * sptr = string;
- *(lines + loffset) = sptr;
- loffset++;
- while(*sptr)
- {
- if(*sptr == '\n')
- {
- *(lines + loffset) = sptr + 1;
- *sptr = '\0';
- loffset++;
- }
- sptr++;
- }
- *(lines + loffset) = NULL;
- return lines;
- }
- void replacechar(char * string, char to, char with)
- {
- while(*string)
- {
- if(*string == to)
- *string = with;
- string++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement