C&C++常用小函数 代码
2010-06-02 17:14
387 查看
格式化输入输出文件
统计字符串中最长的重复字符串
通过递归求字符串中出现的相同字符次数
#include "stdafx.h" #include <stdlib.h> #include "string.h" #include "stdio.h" int main() { FILE *fp; int number[3]; int score[3]; int i = 0; int rece_Number = 0; if((fp = fopen("test.txt","r")) == NULL) { fprintf(stderr,"Error opening file!/n"); } for(i =0;i<3;i++) { fscanf(fp,"%d %d",&number[i],&score[i]);//学号与成绩用空格 } printf("Please input number:/n"); scanf("%d",&rece_Number); for(i = 0;i<3;i++) { if(rece_Number == number[i]) break; } printf("The Number's score is %d/n",score[i]); fclose(fp); return 0; }
统计字符串中最长的重复字符串
#include "stdafx.h" #include "string.h" #include "malloc.h" int main() { int i,j,k,t; int max = 0; char *p = NULL; char str[42] = "2xtuaojy5678zuaojy908puaojy417uaojy356"; char temp[8]; char result[8]; memset(result,0,8); p = str; for(i=0;i <42;i++) { for(j=i+1;j <42;j++) { if(*(p+i)== *(p+j)) { t = i; k = 0; memset(temp,0,8); //只要找到了相同的首字符,就开始比较直到没有相同的字符,记录当前字符串,看是否是最长的 while(*(p+t)== *(p+j)) { temp[k] = *(p+t);//不断比较后续的字符 t++; j++; k++; } if(k > max)//将最长的字符串赋值给result { max = strlen(temp); strcpy(result,temp); } } } } //打印出结果在上述字符串中最长的是 "zuaojy" printf("重复字符串最长的是 %s,长度为 %d /n",result,max); return 0; }
通过递归求字符串中出现的相同字符次数
int count_times(char ch,const char* str) { int count = 0; if(*str == '/0') { count = 0; } else { if((*str)==ch) { count = 1+count_times(ch,str+1); } else { count = count_times(ch,str+1); } } return count; }
相关文章推荐
- Java String 函数常用操作 & format() 格式化输出,代码详解
- [C++基础]队列<queue>中的常用函数
- c++中string常用函数整理<引用>
- C++库常用函数一览(<string><cctype><algorithm><cmath><cstdlib><iomanip><numeric>)
- C++库常用函数一览(<string><cctype><algorithm><cmath><cstdlib><iomanip><numeric>)
- 【poj 3126】Prime Path 题意&题解&代码(C++)
- c++ 二分查找的函数 lower_bound & upper_bound & binary_search
- C++库常用函数一览
- c++基础之string常用函数
- strstr()函数c++代码实现实例及运行结果
- C++ 输出代码所在的文件、行数以及函数名称
- C/C++ 常用调试函数及技巧(不断更新中)
- 4000 C++使用Mysql的详细步骤及各个常用方法的代码演示:select,insert,update,delete
- 【poj 3461】 Oulipo 中文题意&题解&代码(C++)
- 礼拜四log~java函数replaceAll报错解决方案 & 一些常用jq方法 & java函数addAll
- linux网络编程常用函数详解与实例(socket-->bind-->listen-->accept)
- 深度学习常用激活函数之— Sigmoid & ReLU & Softmax
- 【转】c++ map 函数
- c++常用的字符串操作函数
- OC代码调用C++代码的回调函数步骤