Is there a way to split a string every 30 characters and store them in smaller strings without looking for a token?

>Is there a way
Yup, there sure is. It's even easier than looking for tokens because you only need to keep a character count (and look for the end of the string, but that goes without saying).

>Is there a way
Yup, there sure is. It's even easier than looking for tokens because you only need to keep a character count (and look for the end of the string, but that goes without saying).

well thats great. can you fill me in as to how to go about doing such a thing????

>can you fill me in as to how to go about doing such a thing????
Yes, I can.

Oh, you mean you really want me to tell you. Why didn't you say so? There are a number of ways, but I'll assume that because you're asking this question, you're pretty low on the totem pole in terms of C experience. In that case, it's probably best to do something like this:

const char *src = "this is a test";
char split[100][100] = {0};
size_t row = 0;
size_t col = 0;
size_t i;

for ( i = 0; src[i] != '\0'; i++ ) {
  if ( col == 3 ) {
    col = 0;
    ++row;
  }

  split[row][col++] = src[i];
}

for ( i = 0; i <= row; i++ )
  printf ( ">%s<\n", split[i] );

It's far from elegant, but has the benefit of simplicity.

and here's a variation that's even less elegant, but more simple, assuming you can use standard C libraries

#include <string.h> 

char longString[3000], splitString[100][30];
int stringLen, index;

// "longString" is filled with a null-terminated string of chars

stringLen = strlen(longString);
index = 0;

while (index*30 < stringLen)
{
    strncpy(splitString[index], &longString[index*30], 30);
    index++;
}

note that the split strings will not be null-terminated, the unused array elements are uninitialized, the use of arbitrary "magic numbers" (30), and the complete lack of error checking make this entirely unacceptable for commercial or industrial use. i give it here for illustrative purposes only.


.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.