Online Judge hdu1073 字符串操作

http://acm.hdu.edu.cn/showproblem.php?pid=1073

以后的串操作还是靠char最靠谱。。这是个超水的题。

#include <iostream>
using namespace std;
#define MAX 5003
char temp[MAX],a[MAX],b[MAX];

void trim(char *s){   //这才是trim的王道函数,一个char走遍全天下。。
	char temp[MAX];
	int i,j=0;
	strcpy(temp,"\0");
	for (i=0;i<strlen(s);i++)
		if (s[i]!=' '&&s[i]!='\t'&&s[i]!='\n'){
			temp[j]=s[i];
			j++;
		}
	temp[j]='\0';
	strcpy(s,temp);
}

void Compare(char *a,char *b){
	if (strcmp(a,b)==0){
		printf("Accepted\n");
		return ;
	}
	trim(a);
	trim(b);
	if (strcmp(a,b)==0){
		printf("Presentation Error\n");
		return ;
	}
	else {
		printf("Wrong Answer\n");
		return ;
	}
}

void input(char *s){
	char temp[MAX];
	strcmp(temp,"\0");
	gets(temp);
	if (strcmp(temp,"START")==0){
		gets(temp);
		while(strcmp(temp,"END")!=0){   //回车可以pass过gets和getchar,而%s和cin会被挡住
			if (strlen(temp)==0)    //pass过后,gets得到的字符串为空,并没有实际接受回车。
				strcat(s,"\n");
			else strcat(s,temp);
			gets(temp);
		}
	}
}

int main(){
	int n;
	scanf("%d",&n);
	getchar();
	while (n--){
		strcpy(a,"\0");
		strcpy(b,"\0");
		input(a);
		input(b);
		Compare(a,b);
	}
	return 0;
}
顺便复习一下有关char的几个函数,从网上找到个一个比较全面的函数操作。

程序开头要声明
#include <string.h>
 
函数名: stpcpy
功  能: 拷贝一个字符串到另一个
用  法: char *stpcpy(char *destin, char *source);
程序例:
 
#include <stdio.h>
#include <string.h>
 
int main(void)
{
   char string[10];
   char *str1 = "abcdefghi";
 
   stpcpy(string, str1);
   printf("%s\n", string);
   return 0;
}
 
 
 
 
函数名: strcat
功  能: 字符串拼接函数
用  法: char *strcat(char *destin, char *source);
程序例:
 
#include <string.h>
#include <stdio.h>
 
int main(void)
{
   char destination[25];
   char *blank = " ", *c = "C++", *Borland = "Borland";
 
   strcpy(destination, Borland);
   strcat(destination, blank);
   strcat(destination, c);
 
   printf("%s\n", destination);
   return 0;
}
 
 

函数名: strcmp
功  能: 串比较
用  法: int strcmp(char *str1, char *str2);
看Asic码,str1>str2,返回值 > 0;两串相等,返回0
程序例:
 
#include <string.h>
#include <stdio.h>
 
int main(void)
 {
    char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";
    int ptr;
 
    ptr = strcmp(buf2, buf1);
    if (ptr > 0)
       printf("buffer 2 is greater than buffer 1\n");
    else
       printf("buffer 2 is less than buffer 1\n");
 
    ptr = strcmp(buf2, buf3);
    if (ptr > 0)
       printf("buffer 2 is greater than buffer 3\n");
    else
       printf("buffer 2 is less than buffer 3\n");
 
    return 0;
 }
  
函数名: strcpy
功  能: 串拷贝
用  法: char *strcpy(char *str1, char *str2);
程序例:
 
#include <stdio.h>
#include <string.h>
 
int main(void)
 {
    char string[10];
    char *str1 = "abcdefghi";
 
    strcpy(string, str1);
    printf("%s\n", string);
    return 0;
 }

其余函数见http://www.byvoid.com/blog/c-string/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值