包含多个子串的字符串,用逗号隔开,格式化输出这些子串,比如N行4列输出

问题:一个包含多个子串的字符串,期间用逗号隔开,格式化输出这些子串,比如N行4列输出。(每列按字串的最大长度输出,不足补-号)

前几天在上课,一同学问我这个问题,说是他们C语言竞赛时候出的一个问题,说他试着用三维数组去解决,但是很久都未成功。我和他下课讨论了一会,开始说用一个二维指针数组,后来,我发现,如果仅仅只要格式化输出,用一维数组完全就可以解决的。算法啊,算法是一个程序的灵魂。

/*Author:shizhixin
Email:szhixin@gmail.com
Blog:http://blog.youkuaiyun.com/ShiZhixin
Date:Nov 28,2011


input a string which include some substring separated by ',' 
the program is to print these substring with M rows and N cols, 
meanwihle, align these substrings. if the lack, padded with '-' 
*/


#include "stdafx.h"
#include <iostream>
using namespace std;


#define ERROR -1;


//get the count of sub-string in string, the substring is separated by ','
int count_string(char* string)
{
int count = 0;
char* pos = string;
while(*pos != '\0')
{
if (*(pos++) == ',')
{
count++;
}
} 
return count+1;
}


//get the length of every substring
//out: len_per_str
//in: input_string
void get_len_per_str(int* len_per_str, char* input_string)
{
int pos_len = 0;
char* pos_input = input_string;
while (*pos_input !='\0')
{
if (*(pos_input++) == ',')
{
pos_len ++;
}
else
{
len_per_str[pos_len]++;
}
}
}


//the array(one-dim) is divided into N rows and col columns, we get the max number in each column 
//out: max_col, use to store the max number in each column
//out: col, the columns
//in: array, the one-dim array
//in: the length of array
void get_max_col_array(int* max_col, int col, int* array, int len)
{
int i;
for (i=0; i<col; i++)
{
max_col[i]= 0;
}
int ind = 1;
for (i = 0; i <  len; i++)
{
ind = i%col;
if (max_col[ind]<array[i])
{
max_col[ind] =  array[i];
}


}


}


//overload some print functions
void print_output(char* printf_string, int array[], int size)
{
cout<<printf_string<<":";
for (int i=0; i<size; i++)
{
cout<<array[i]<<" ";
}
cout<<endl;
}


void print_output(char* printf_string)
{
cout<<printf_string<<":"<<endl;
}


void print_output(char* printf_string, char* string)
{
cout<<printf_string<<":"<<endl;
cout<<string<<endl;
}


void print_output(char* printf_string, int num)
{
cout<<printf_string<<":"<<num<<endl;
}


int main(int argc, char* argv[])
{
const int COL_NUM = 4; //the number of string in a line to print
int i,j; //for


char* input_string = "shi,zhixin,is a,good boy!,this is,my blog,at,csdn!,thanks,your,attention";
print_output("the input string is", input_string);


int len_str = strlen(input_string);//the length of input string
print_output("len_str", len_str);

int count_str = count_string(input_string);//the count of sub-string in input string
print_output("count_str", count_str);


int* len_per_str = new int[count_str];
memset(len_per_str, 0, count_str*sizeof(int));
get_len_per_str(len_per_str, input_string);    //the length of each substring
print_output("len_per_str", len_per_str, count_str);


int* pmax_col = new int[COL_NUM];
get_max_col_array(pmax_col, COL_NUM, len_per_str, count_str); //the max size of substring in each column
print_output("pmax_col", pmax_col, COL_NUM);




print_output("the alignment of strings is");
int start_ind = 0;
for (i=0; i<count_str; i++)
{
int ind = i%COL_NUM;
char ch;
for (j=0; j<pmax_col[ind]; j++)
{
if (j < len_per_str[i])
{
ch = input_string[start_ind];
start_ind++;
}
else
ch = '-';
cout<<ch;
}
cout<<'|';
start_ind++;
if (ind == COL_NUM-1)
{
cout<<endl;
}

}


cout<<endl;
delete pmax_col;
delete[] len_per_str;


return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值