第16周项目2--用指针玩字符串(字符串的比较)
来源:互联网 发布:移动4g是什么网络 编辑:程序博客网 时间:2024/05/24 02:56
/* * Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作 者:刘畅 * 完成日期:2014 年 12 月 13 日 * 版 本 号:v1.0 * * 问题描述:用指针作形参,其核心是实现int pstrcemp(char *str)函数。; * 输入描述:无需输入; * 程序输出:输出要求输出的。
#include<iostream>using namespace std;int pstrcmp(const char *str1,const char *str2);int main(){ char s1[50]="nice to meet you. "; char s2[50]="nice to meet you. "; char s3[50]="nice to meet you too. "; cout<<"(如果相同,则输出1,否则输出-1)。经比较,依次为:"<<endl; cout<<pstrcmp(s1,s2)<<endl; cout<<pstrcmp(s1,s3)<<endl; cout<<pstrcmp(s2,s3)<<endl; return 0;}int pstrcmp(const char *str1,const char *str2){ int n=0,i; for(i=0;;i++) { if(*(str1+i)!=*(str2+i)) { return -1; break; } if(*(str1+i)==*(str2+i)) { n++; if (*(str1+i)=='\0'&&*(str2+i)=='\0') break; } } return 1;}运行结果:
学习心得:
断断续续写了很久,总算搞定了,不过字符串的比较是以ASCII码值的大小作比较的么。写了两种版本的代码,一种是这样的,还有一种是以*(st1r+i)大于或小于或等于*(str2+i)为判断分别输出1,0,-1三种情况的,不过似乎有些问题就简化成了这个,但编完这个那个似乎又通了,呵呵,继续奋斗中!!!
2 0
- 第16周项目2--用指针玩字符串(字符串的比较)
- 第16周项目2-用指针玩字符串(任务七:比较字符串)
- 第16周项目2-用指针玩字符串(7)字符串比较
- 第16周项目2--用指针玩字符串(实现字符串的连接)
- 第16周项目2-用指针玩字符串(5)删除字符串前的空格
- 第16周项目2-用指针玩字符串(计算单词的个数-指针作形参)
- C++第17周项目2——用指针玩字符串(7)比较
- 第16周项目2 用指针玩字符串2
- 第16周项目2(4)-用指针玩字符串
- 第16周项目2(5)-用指针玩字符串
- 第16周项目2(6)-用指针玩字符串
- 第16周项目2(7)-用指针玩字符串
- 第16周项目2-用指针玩字符串
- 第16周【项目 2-用指针玩字符串】①
- 第16周【项目 2-用指针玩字符串】②
- 第16周 项目2-用指针玩字符串(1)
- 第16周【项目 2-用指针玩字符串】③
- 第16周【项目 2-用指针玩字符串】④
- HDU 2608 0 or 1(数论)
- 常见的操作系统使用的文件系统整理
- 基于虚拟机的centos版本linux操作系统如何联网
- Eclipse快捷键
- 浅谈java集合类型的相关操作
- 第16周项目2--用指针玩字符串(字符串的比较)
- 大数据处理之道 (MATLAB 篇)
- poj解题报告——1321
- highcharts 图表下钻drilldown
- Android -- Sharedpreferences
- C++primer(第二遍) 补第六章最后一题
- AllWinner board 笔记
- 基于adaboost的人脸检测方法
- ISBN