Monday, July 9, 2007

Write a simple piece of code to split a string at equal intervals.

Suppose you have a big string


This is a big string which I want to split at equal intervals, without caring about the words.


Now, to split this string say into smaller strings of 20 characters each, try this


#define maxLineSize 20

split(char *string)
{
int i, length;
char dest[maxLineSize + 1];

i = 0;
length = strlen(string);

while((i+maxLineSize) <= length)
{
strncpy(dest, (string+i), maxLineSize);
dest[maxLineSize - 1] = '\0';
i = i + strlen(dest) - 1;
printf("\nChunk : [%s]\n", dest);
}

strcpy(dest, (string + i));
printf("\nChunk : [%s]\n", dest);
}

11 comments:

Anonymous said...

"while((i+maxLineSize) <= length)
{
strncpy(dest, (string+i), maxLineSize);
dest[maxLineSize - 1] = '\0';
i = i + strlen(dest) - 1;
printf("\nChunk : [%s]\n", dest);
}"

"i = i + strlen(dest) - 1;" might be an overkill, since you should always be advancing "maxLineSize", and strlen() is expansive ...

Anonymous said...

Another efficient way of doing the same functionality....
(sorry for no comments though)

#include "stdio.h"
#include "memory.h"
#include "TCHAR.H"

const int SplitInterval = 5;

void Split(char InputString[])
{
char pTemp[SplitInterval+1];
memset(pTemp,'\0',SplitInterval+1);
int iLength = strlen(InputString);
int index = 0;
while(index < iLength)
{
strncpy(pTemp,(InputString+index),5);
index+= SplitInterval;
printf("Chunk : [%s] \n", pTemp);
}
}

void main()
{
char pInput[] = "Let us make world a better place to live in by contributing as much as possible.";
Split(pInput);
}

Anonymous said...

str[]="abcdefghijklmnopqrstuvwxyz";
int i,j;
intk=0;
while(i<strlen(str))
{
for(j=0;j<split;j++)
{
if(str[k]!='\0')
{
str1[k++]=str[i++];
}
str1[k++]='\0';
}
}
}

Anonymous said...

OUTPUT:

ab cd ef gh ij kl mn op qr st uv wx yz

Anonymous said...

// Return the number of strings formed
int split_string(const char* sz, const int nSize)
{
char dest[nSize + 1];
int nCount = 0;
int nSrcIndex = 0;
int nDstIndex = 0;

dest[nSize] = '\0';
while (*sz)
{
dest[nDstIndex] = *sz;

if ((nSize - 1) == nDstIndex)
{
cout << dest << endl;
nCount++;
}

nSrcIndex++; sz++;
nDstIndex = nSrcIndex % nSize;
}

if (nDstIndex > 0)
{
dest[nDstIndex] = '\0';
cout << dest << endl;
nCount++;
}

return nCount;
}

vikas kumar jain said...

#include
#include
#include
void main()
{
char str[]="vikaskumarjain";
char str1[18];
int i=0 ,j,k=0;
int split=2;
//clrscr();
while(i<strlen(str))
{
for(j=0;j<split;j++)
{
str1[j++]=str[i++];
printf("%c",str1[--j]);
}
//str1[j++]='\0';
printf(" ");
}



getch();
}

vikas kumar jain said...

#include
#include
#include
void main()
{
char str[]="vikaskumarjain";
char str1[18];
int i=0 ,j,k=0;
int split=2;
//clrscr();
while(i<strlen(str))
{
for(j=0;j<split;j++)
{
str1[j++]=str[i++];
printf("%c",str1[--j]);
}
//str1[j++]='\0';
printf(" ");
}



getch();
}

vikas kumar jain said...

#include
#include
#include
void main()
{
char str[]="vikaskumarjain";
char str1[18];
int i=0 ,j,k=0;
int split=2;
//clrscr();
while(i<strlen(str))
{
for(j=0;j<split;j++)
{
str1[j++]=str[i++];
printf("%c",str1[--j]);
}
//str1[j++]='\0';
printf(" ");
}



getch();
}

Unknown said...
This comment has been removed by the author.
Unknown said...

This code will split the string at equal intervals

#include
#include
#define split 3
using namespace std;
int main()
{
string s="PROGRAMMING",ss;
int start=0,i;
for(i=1;i<=(s.length()/split)+(s.length()%split?1:0);i++)
{
ss=s.substr(start,split);
start+=split;
cout<<ss<<" ";
}
}

Unknown said...

Thanks for the info. It is very helpful.
RegardsEducational site
Get jobs info at Educational site