Posted on 2008-06-16 06:33
nt05 阅读(159)
评论(0) 编辑 收藏 引用 所属分类:
windows nt
#include <iostream>
using namespace std;
int main()
{
char str[20];
cout<<"Input :";
cin.get(str,19);
if (strcmp(str, "win") == 0) // 用strcmp判断
cout<<str<<endl;
system("PAUSE");
return 0;
}
【jixingzhong】:
char str[20];
cout<<"Input :";
cin.get(str,19);
cout<<str<<endl;
==》
string str;
cout<<"Input :";
getline(cin, str);
if(str=="win") cout<<str<<endl; //string operater=
【jixingzhong】:
如果必须是 char,
使用字符串操作函数即可:
函数名称: strcmp
函数原型: int strcmp(char * str1,char * str2);
函数功能: 比较两个字符串str1,str2.
函数返回: str1<str2,返回负数; str1=str2,返回 0; str1>str2,返回正数.
参数说明:
所属文件: <string.h>
#include <string.h>
#include <stdio.h>
int main()
{
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;
}
或者
函数名称: strncmp
函数原型: int strncmp(char *str1,char *str2,int count)
函数功能: 对str1和str2中的前count个字符按字典顺序比较
函数返回: 小于0:str1<str2,等于0:str1=str2,大于0:str1>str2
参数说明: str1,str2-待比较的字符串,count-比较的长度
所属文件: <string.h>
#include <string.h>
#include <stdio.h>
int main()
{
int ptr;
char *buf1="aaabbb",*buf2="bbbccc",*buf3="ccc";
ptr=strncmp(buf2,buf1,3);
if (ptr>0)
printf("buffer 2 is greater than buffer 1");
else
printf("buffer 2 is less than buffer 1");
ptr=strncmp(buf2,buf3,3);
if (ptr>0)
printf("buffer 2 is greater than buffer 3");
else
printf("buffer 2 is less than buffer 3");
return(0);
}
或者 memcpy / memccpy
【wanfustudio】:
strncmp