c++ - How to debug this code for splitting an array by space? -
i need write program sentence , split words delimiter(space);so i've wrote code below doesn't seem working properly. idea's how debug code? in advance help. here's come far:
#include <iostream> using namespace std; const int buffer_size=255; int main() { char* buffer; buffer=new char[255]; cout<<"enter statement:"<<endl; cin.getline(buffer,buffer_size); int q=0, numofwords=1; while(buffer[q] != '\0'){ if(buffer[q]==' ') numofwords ++; q ++; } char** wordsarray; wordsarray= new char* [numofwords]; int lenofeachword=0, num=0; int* sizeofwords=new int [numofwords]; for(int i=0;i<q;i++){ if(buffer[i]==' ') { sizeofwords[num]=lenofeachword; wordsarray[num]=new char[lenofeachword]; num++; }else{ lenofeachword++; } } sizeofwords[num]=lenofeachword; wordsarray[num]=new char[lenofeachword]; int k=0; for(int i=0; i<numofwords;i++){ for(int j=0;j<sizeofwords[i];j++) { wordsarray[i][j]=buffer[k]; k++; } k++; } for(int i=0; i<numofwords;i++){ for(int j=0;j<sizeofwords[i];j++) { cout<<wordsarray[i][j]; } cout<<endl; } }
#include <iostream> #include <stdio.h> #include <cstring> using namespace std; int main() { int size, j; char s[1005]; gets(s); scanf("%d", &size); j=0; for(int i=size; i<strlen(s); i+=size) { for(; j<i; j++) { printf("%c", s[j]); } printf(" "); } return 0; }
Comments
Post a Comment