tag:blogger.com,1999:blog-7662544687317968079.post4042348207634349928..comments2024-03-19T00:15:43.543-07:00Comments on Interview Questions: Write a simple piece of code to split a string at equal intervals.Vijay Agrawalhttp://www.blogger.com/profile/16856170291286959451noreply@blogger.comBlogger11125tag:blogger.com,1999:blog-7662544687317968079.post-68298125071337400612012-10-31T15:49:21.498-07:002012-10-31T15:49:21.498-07:00Thanks for the info. It is very helpful.
RegardsEd...Thanks for the info. It is very helpful.<br />Regards<a href="http://studentsarea.com/" rel="nofollow">Educational site</a><br />Get jobs info at <a href="http://studentsarea.com/" rel="nofollow">Educational site</a><br />Anonymoushttps://www.blogger.com/profile/01883712208510168868noreply@blogger.comtag:blogger.com,1999:blog-7662544687317968079.post-77023170753797662442012-07-08T08:49:11.515-07:002012-07-08T08:49:11.515-07:00This code will split the string at equal intervals...This code will split the string at equal intervals<br /><br />#include<br />#include<br />#define split 3<br />using namespace std;<br />int main()<br />{<br /> string s="PROGRAMMING",ss;<br /> int start=0,i;<br /> for(i=1;i<=(s.length()/split)+(s.length()%split?1:0);i++)<br /> {<br /> ss=s.substr(start,split);<br /> start+=split;<br /> cout<<ss<<" ";<br /> }<br />}Anonymoushttps://www.blogger.com/profile/02371710275929442934noreply@blogger.comtag:blogger.com,1999:blog-7662544687317968079.post-22217855399998128452012-07-08T08:48:08.590-07:002012-07-08T08:48:08.590-07:00This comment has been removed by the author.Anonymoushttps://www.blogger.com/profile/02371710275929442934noreply@blogger.comtag:blogger.com,1999:blog-7662544687317968079.post-36321765963753157862011-12-08T23:50:58.289-08:002011-12-08T23:50:58.289-08:00#include
#include
#include
void main()
{
char str...#include<br />#include<br />#include<br />void main()<br />{<br /> char str[]="vikaskumarjain";<br /> char str1[18];<br /> int i=0 ,j,k=0;<br /> int split=2; <br /> //clrscr();<br /> while(i<strlen(str))<br /> {<br /> for(j=0;j<split;j++)<br /> {<br /> str1[j++]=str[i++];<br /> printf("%c",str1[--j]);<br /> }<br /> //str1[j++]='\0';<br /> printf(" ");<br /> }<br /><br /><br /><br /> getch();<br />}vikas kumar jainhttps://www.blogger.com/profile/05919790235847999947noreply@blogger.comtag:blogger.com,1999:blog-7662544687317968079.post-84142768961273347562011-12-08T23:50:05.614-08:002011-12-08T23:50:05.614-08:00#include
#include
#include
void main()
{
char str...#include<br />#include<br />#include<br />void main()<br />{<br /> char str[]="vikaskumarjain";<br /> char str1[18];<br /> int i=0 ,j,k=0;<br /> int split=2; <br /> //clrscr();<br /> while(i<strlen(str))<br /> {<br /> for(j=0;j<split;j++)<br /> {<br /> str1[j++]=str[i++];<br /> printf("%c",str1[--j]);<br /> }<br /> //str1[j++]='\0';<br /> printf(" ");<br /> }<br /><br /><br /><br /> getch();<br />}vikas kumar jainhttps://www.blogger.com/profile/05919790235847999947noreply@blogger.comtag:blogger.com,1999:blog-7662544687317968079.post-82741649233798516592011-12-08T23:49:47.950-08:002011-12-08T23:49:47.950-08:00#include
#include
#include
void main()
{
char str...#include<br />#include<br />#include<br />void main()<br />{<br /> char str[]="vikaskumarjain";<br /> char str1[18];<br /> int i=0 ,j,k=0;<br /> int split=2; <br /> //clrscr();<br /> while(i<strlen(str))<br /> {<br /> for(j=0;j<split;j++)<br /> {<br /> str1[j++]=str[i++];<br /> printf("%c",str1[--j]);<br /> }<br /> //str1[j++]='\0';<br /> printf(" ");<br /> }<br /><br /><br /><br /> getch();<br />}vikas kumar jainhttps://www.blogger.com/profile/05919790235847999947noreply@blogger.comtag:blogger.com,1999:blog-7662544687317968079.post-66945484207065253032011-12-02T00:31:17.259-08:002011-12-02T00:31:17.259-08:00// Return the number of strings formed
int split_s...// Return the number of strings formed<br />int split_string(const char* sz, const int nSize)<br />{<br /> char dest[nSize + 1];<br /> int nCount = 0;<br /> int nSrcIndex = 0;<br /> int nDstIndex = 0;<br /><br /> dest[nSize] = '\0';<br /> while (*sz)<br /> {<br /> dest[nDstIndex] = *sz;<br /><br /> if ((nSize - 1) == nDstIndex)<br /> {<br /> cout << dest << endl;<br /> nCount++;<br /> }<br /><br /> nSrcIndex++; sz++;<br /> nDstIndex = nSrcIndex % nSize;<br /> }<br /><br /> if (nDstIndex > 0)<br /> {<br /> dest[nDstIndex] = '\0';<br /> cout << dest << endl;<br /> nCount++;<br /> }<br /><br /> return nCount;<br />}Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7662544687317968079.post-85687154257544803052011-10-15T10:58:39.974-07:002011-10-15T10:58:39.974-07:00OUTPUT:
ab cd ef gh ij kl mn op qr st uv wx yzOUTPUT:<br /><br />ab cd ef gh ij kl mn op qr st uv wx yzAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-7662544687317968079.post-63863115593970285542011-10-15T10:57:10.642-07:002011-10-15T10:57:10.642-07:00str[]="abcdefghijklmnopqrstuvwxyz";
int ...str[]="abcdefghijklmnopqrstuvwxyz";<br />int i,j;<br />intk=0;<br />while(i<strlen(str))<br />{<br /> for(j=0;j<split;j++)<br /> {<br /> if(str[k]!='\0')<br /> {<br /> str1[k++]=str[i++];<br /> }<br /> str1[k++]='\0';<br /> }<br />}<br />}Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7662544687317968079.post-37863842144444364782008-01-06T10:44:00.000-08:002008-01-06T10:44:00.000-08:00Another efficient way of doing the same functional...Another efficient way of doing the same functionality....<BR/>(sorry for no comments though)<BR/><BR/>#include "stdio.h"<BR/>#include "memory.h"<BR/>#include "TCHAR.H"<BR/><BR/>const int SplitInterval = 5;<BR/><BR/>void Split(char InputString[])<BR/>{<BR/> char pTemp[SplitInterval+1];<BR/> memset(pTemp,'\0',SplitInterval+1);<BR/> int iLength = strlen(InputString);<BR/> int index = 0;<BR/> while(index < iLength)<BR/> {<BR/> strncpy(pTemp,(InputString+index),5); <BR/> index+= SplitInterval;<BR/> printf("Chunk : [%s] \n", pTemp);<BR/> }<BR/>}<BR/><BR/>void main()<BR/>{<BR/> char pInput[] = "Let us make world a better place to live in by contributing as much as possible."; <BR/> Split(pInput);<BR/>}Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7662544687317968079.post-85297971119344131272007-09-08T18:10:00.000-07:002007-09-08T18:10:00.000-07:00"while((i+maxLineSize) {strncpy(dest, (string+i), ..."while((i+maxLineSize) <= length)<BR/>{<BR/>strncpy(dest, (string+i), maxLineSize);<BR/>dest[maxLineSize - 1] = '\0';<BR/>i = i + strlen(dest) - 1;<BR/>printf("\nChunk : [%s]\n", dest);<BR/>}"<BR/><BR/>"i = i + strlen(dest) - 1;" might be an overkill, since you should always be advancing "maxLineSize", and strlen() is expansive ...Anonymousnoreply@blogger.com