13

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"; in the second, I should get "TRIN"; in the third, I should get "GHAS". How can I do that in C?

JYelton
  • 35,664
  • 27
  • 132
  • 191
shinshin32
  • 139
  • 2
  • 2
  • 6
  • 7
    What have you tried? Please show a little effort on your homework before asking for help. – antlersoft Jul 13 '11 at 12:56
  • 1
    possible duplicate of [Strings in c, how to get subString](http://stackoverflow.com/questions/2114377/strings-in-c-how-to-get-substring) – Lekensteyn Sep 12 '11 at 20:11

4 Answers4

18

If the task is only copying 4 characters, try for loops. If it's going to be more advanced and you're asking for a function, use strncpy.

strncpy(sub1, baseString, 4);
strncpy(sub1, baseString+4, 4);
strncpy(sub1, baseString+8, 4);

or

for(int i=0; i<4; i++)
    sub1[i] = baseString[i];
sub1[4] = 0;
for(int i=0; i<4; i++)
    sub2[i] = baseString[i+4];
sub2[4] = 0;
for(int i=0; i<4; i++)
    sub3[i] = baseString[i+8];
sub3[4] = 0;

Prefer strncpy if possible.

user4581301
  • 33,082
  • 7
  • 33
  • 54
holgac
  • 1,509
  • 1
  • 13
  • 25
  • 2
    You've got the order wrong: it's `strcpy(dest,src)`. Also, you need to copy only 4 chars, so `strcpy` is not the right tool. Use `strncpy`. – lhf Jul 13 '11 at 12:59
  • yeah, I noticed and changed it to memcpy. strncpy is also possible. – holgac Jul 13 '11 at 13:01
  • If the OP wants a *substring* then you need to add a terminating `'\0'`. – lhf Jul 13 '11 at 13:03
  • 1
    Just remembered strncpy works better with null terminated strings. So, strncpy is the way. – holgac Jul 13 '11 at 13:03
  • *"If the task is only copying 4 characters, try for loops."* .. why not always use the the function ?, it's tested , reliable and even less coding than a loop. 4 characters or 4 Million characters is no difference than the loop counter number. – Accountant م Apr 03 '19 at 00:11
  • @Accountantم Agreed. I would also like to know why. – MilkyWay90 Aug 23 '19 at 01:04
4
#include <stdio.h>
#include <string.h>

int main() {
    char src[] = "SexDrugsRocknroll";
    char dest[5] = { 0 }; // 4 chars + terminator */
    int len = strlen(src);
    int i = 0;

    while (i*4 < len) {
        strncpy(dest, src+(i*4), 4);
        i++;

        printf("loop %d : %s\n", i, dest);
    }
}
Andrejs Cainikovs
  • 27,428
  • 2
  • 75
  • 95
0

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);
}
pmg
  • 106,608
  • 13
  • 126
  • 198
0
char originalString[] = "THESTRINGHASNOSPACES";

    char aux[5];
    int j=0;
    for(int i=0;i<strlen(originalString);i++){
        aux[j] = originalString[i];
        if(j==3){
            aux[j+1]='\0'; 
            printf("%s\n",aux);
            j=0;
        }else{
            j++;
        }
    }
Alex Terente
  • 12,006
  • 5
  • 51
  • 71