// split.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "string.h"
#define MAX_ITEM 32768namespace strsplite {
// Exchange from src to dest in input string char* exchange(char* input, char src, char dest){ char* change = new char[strlen(input)+1]; char* change_init = change; char* input_init = input;
while ((*input) != '/0') { if ((*input) == src ) *change = dest; else *change = *input; change++; input++; } *change = '/0'; change = NULL; input = input_init; return change_init; }
// align input string char* align_right(char* input,int width){ int length = strlen(input); char* change = new char[length+1]; int current =0,pre_space = -1, this_line_number = 0; while (current<length) { if (input[current]=='/n'){ this_line_number = 0; pre_space = -1; } if (input[current]==' '&&this_line_number<=width) pre_space = current; if (this_line_number>width) { if (pre_space != -1) { this_line_number = 0; current = pre_space + 1; change[pre_space] = '/n'; pre_space = -1; } else { printf("too narrow!"); return NULL;} } change[current] = input[current]; current++; this_line_number++; } change[current] = '/0'; //printf("%s",change); return change; }
// split a string by a character int split(const char split,const char* input, char** out, int out_slot) { int item_number = 0; int input_size = strlen(input); int current = 0;
while (current<input_size) { // Get next! int this_end=-1,i = current; while (i<input_size&&this_end==-1){ if (input[i] == '/n' || input[i] == split) this_end = i; i++; } if (this_end == -1) this_end = input_size; int this_long = this_end - current; out[item_number] = new char[this_long+1]; for (int i=0;i<this_long;i++) out[item_number][i]=input[current+i]; out[item_number][this_long]='/0'; //printf("%s/n",out[item_number]); item_number++; if (item_number>=out_slot) return -1; //no slot to use current = this_end+1; } return item_number; }
char* deleteNote(const char* input) { printf("-- original --/n%s/n",input); int item_number = 0; int input_size = strlen(input); int current = 0; char* out = new char[input_size+1]; int status = 0; char* out_init = out;
while (current<input_size) { // Get next! if (current+1<input_size){ if (input[current]=='/' && input[current+1]=='/' && status == 0){ status = 1; current += 2; } if (input[current] == '/' && input[current+1] == '*' && status == 0) { status = 2; current += 2; } if (input[current] == '*' && input[current+1] == '/' && status == 2) { status = 0; current+=2; } } if (input[current]=='/n' && status ==1){ status = 0; } if (status==0){ (*out) = input[current]; out++; } current++; } printf("-- removed --/n%s/n",out_init); return out_init; }
}
int _tmain(int argc, _TCHAR* argv[]){ char* out[MAX_ITEM]; int i; for (i=0;i<MAX_ITEM;i++) out[i]=NULL; strsplite::deleteNote("#define abc 123/n void main(){/n printf(/"hello world/"); // print it /n}/n /* if you love me/n if you hate me/n you can save me/n */"); char* inp0 = "about 123 abc/n2234 game/nsave"; printf("/n-- original --/n%s/n/n",inp0); char* inp = strsplite::exchange(inp0,'/n',' '); //Remove /n //delete [] inp ->OK inp0 ->Wrong; printf("/n-- exchange [enter] into [space] --/n%s/n/n",inp); printf("/n-- split by [space] --/n"); int total_items = strsplite::split(' ',inp,out,MAX_ITEM); for (int i=0;i<total_items;i++) printf("%s/n",out[i]);
char* inp1 = strsplite::align_right(inp0,10); // width = 10 printf("/n-- aligned original--/n%s/n/n",inp1); scanf("%d",&i); return 0;}