How to remove \n or \t from a given string in C?

后端 未结 5 1815
野性不改
野性不改 2021-01-18 01:27

How can I strip a string with all \\n and \\t in C?

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-18 01:52

    This is a c string function that will find any character in accept and return a pointer to that position or NULL if it is not found.

    #include 
    
    char *strpbrk(const char *s, const char *accept);
    

    Example:

    char search[] = "a string with \t and \n";
    
    char *first_occ = strpbrk( search, "\t\n" );
    

    first_occ will point to the \t, or the 15 character in search. You can replace then call again to loop through until all have been replaced.

提交回复
热议问题