How to get substring in C

后端 未结 4 705
北海茫月
北海茫月 2020-12-09 11:52

I have a string, let\'s say \"THESTRINGHASNOSPACES\".

I need something that gets a substring of 4 characters from the string. In the first call, I should get \"THES\

4条回答
  •  生来不讨喜
    2020-12-09 12:14

    If you just want to print the substrings ...

    char s[] = "THESTRINGHASNOSPACES";
    size_t i, slen = strlen(s);
    for (i = 0; i < slen; i += 4) {
      printf("%.4s\n", s + i);
    }
    

提交回复
热议问题