POJ——字符串插入(AC)

次题比较简单,直接贴源码


/*
************************************************************************************************************************
1. Description:
There are two strings, named as “str” and “substr”. The number of characters in “str”is less than 10 and the number of characters in “substr” is 3.
The number of characters does not contain the ending ‘\0’.
We will insert “substr” into “str” after the character with the max ASCII code in“str”. 
We will insert at the firstmax-ASCII-code character if there are not only one.
2. Input:
The input contains many lines, and each line is a test case in the form of:
str substr
3. Output:
Output the results in each case.
4. Example:
 input:
   abcab eee
   12345 555
 output:
   abceeeab
   12345553
**************************************************************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>


#define BUF_SIZE  20
#define SIZE_STR  10
#define SIZE_SUB  3

struct tagNode{
	struct tagNode *next;
	char buf[ SIZE_STR+SIZE_SUB+2 ];
};

char _tmpbuf[SIZE_STR];

int main(void){
	size_t len = BUF_SIZE;
	char *line = (char*)malloc(len); 
	char *str, *substr;
	int i,j;

	struct tagNode *head = (struct tagNode*)malloc(sizeof(struct tagNode));
	struct tagNode *tail = head;
	struct tagNode *nnode = NULL;
	head->next = NULL;

	while( -1 != getline(&line,&len , stdin) ){
		int c = 0;
		int p = 0;
		str = strtok(line," ");
		substr = strtok(NULL," \n");
		
		for ( i=0; str[i] != '\0'; i++ ){
			if ( str[i] > c ){
				c = str[i];
				p = i;
			}
		}
		for ( j=0; j<i-p-1; j++ )
			_tmpbuf[ j ] = str[ p+j+1 ];

		str[ ++p ] = substr[ 0 ];
		str[ ++p ] = substr[ 1 ];
		str[ ++p ] = substr[ 2 ];
		
		for ( i=0; i<j; i++ )
			str[ ++p ] = _tmpbuf[ i ];
		str[ ++p ] = 0;
		/*printf("%s\n",str);*/

		/* list insert */
		nnode = (struct tagNode*)malloc(sizeof(struct tagNode));
		strcpy(nnode->buf,str);
		nnode->next = NULL;
		tail->next = nnode;
		tail = nnode;
	}

	for ( tail=head->next; tail!=NULL; tail=tail->next ){
		printf("%s\n",tail->buf);
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值