获取字符串的N段子串
来源:互联网 发布:根域名有什么用 编辑:程序博客网 时间:2024/05/19 11:20
例如输入:
0 3 5 7 -1
input:0123456789
output:0123567
程序分析:
输入两段字串间隔如:(0 3) (5 7) -1是结束符
/*** This program reads input lines from the standard input and prints** each input line, followed by just some portions of the lines, to** the standard output.**** The first input is a list of column numbers, which ends with a** negative number. The column numbers are paired and specify** ranges of columns from the input line that are to be printed.** For example, 0 3 10 12 -1 indicates that only columns 0 through 3** and columns 10 through 12 will be printed.*/#include <stdio.h>#include <stdlib.h>#include <string.h>#defineMAX_COLS20/* max # of columns to process */#defineMAX_INPUT1000/* max len of input & output lines */intread_column_numbers( int columns[], int max );voidrearrange( char *output, char const *input, int n_columns, int const columns[] );intmain( void ){intn_columns;/* # of columns to process */intcolumns[MAX_COLS];/* the columns to process */charinput[MAX_INPUT];/* array for input line */charoutput[MAX_INPUT];/* array for output line *//*** Read the list of column numbers*/n_columns = read_column_numbers( columns, MAX_COLS );/*** Read, process and print the remaining lines of input.*/while( gets( input ) != NULL ){printf( "Original input : %s\n", input );rearrange( output, input, n_columns, columns );printf( "Rearranged line: %s\n", output );}return EXIT_SUCCESS;}/*** Read the list of column numbers, ignoring any beyond the specified** maximum.*/intread_column_numbers( int columns[], int max ){intnum = 0;intch;/*** Get the numbers, stopping at eof or when a number is < 0.*/while( num < max && scanf( "%d", &columns[num] ) == 1 && columns[num] >= 0 )num += 1;/*** Make sure we have an even number of inputs, as they are** supposed to be paired.*/if( num % 2 != 0 ){puts( "Last column number is not paired." );exit( EXIT_FAILURE );}/*** Discard the rest of the line that contained the final** number.*/while( (ch = getchar()) != EOF && ch != '\n' );return num;}/*** Process a line of input by concatenating the characters from** the indicated columns. The output line is then NUL terminated.*/voidrearrange( char *output, char const *input, int n_columns, int const columns[] ){intcol;/* subscript for columns array */intoutput_col;/* output column counter */intlen;/* length of input line */len = strlen( input );output_col = 0;/*** Process each pair of column numbers.*/for( col = 0; col < n_columns; col += 2 ){intnchars = columns[col + 1] - columns[col] + 1;/*** If the input line isn't this long or the output** array is full, we're done.*/if( columns[col] >= len || output_col == MAX_INPUT - 1 )break;/*** If there isn't room in the output array, only copy** what will fit.*/if( output_col + nchars > MAX_INPUT - 1 )nchars = MAX_INPUT - output_col - 1;/*** Copy the relevant data.*/strncpy( output + output_col, input + columns[col], nchars );output_col += nchars;}output[output_col] = '\0';}
- 获取字符串的N段子串
- 通过抓包获取内涵段子的接口
- 获取内涵段子的python 和 显示图片
- MFC 获取字符串的第n个字符
- 白领的段子
- 韩乔生的经典段子
- 关于招聘的段子
- 段子:美好的事情
- 吐槽的段子
- 老人的段子情缘
- 听歌时收集的段子
- 优美的段子
- 有关程序员的段子
- CString 字符串 按指定字符 分隔 获取第n段的子字符串
- js获取一个字符串中指定字符串第n次出现的位置
- 字符串的n位左旋
- 段子
- 段子
- printk的使用方法----积累
- Tomcat源码分析(九)--Session管理
- Visual Studio 遇到了异常。这可能是由某个扩展导致的。
- windows2003记录远程桌面连接登录日志的方法及注意事项
- andorid 内存优化管理(个人总结附带图片压缩源码)
- 获取字符串的N段子串
- 嵌入式经典面试题-嵌入式程序员必须知道的0x10个基础问题
- VS2010快捷键
- php的几个转义函数
- qt基础
- NAT loopback
- CString 和 LPCTSTR 之间的转换 及 LPSTR、LPWSTR、LPCSTR、LPCWSTR、LPTSTR、LPCTSTR的区分与转化
- 28个Unix/Linux的命令行神器
- [HNOI2004]宠物收养所