算法初级_1:字符
来源:互联网 发布:excle导入进sql 编辑:程序博客网 时间:2024/04/29 10:29
main函数
- c++
#include <iostream>using namespace std;int main (int args, char* argv[]){ int c1 = 'a'; int c2 = 'a'+3; cout<<"c1: \t"<<c1<<endl; cout<<"c2: \t"<<c1<<endl; return 0;}
- c
#include "stdio.h"int main (int args, char* argv[]){ int c1 = 'a'; int c2 = 'a'+3; printf("%c - %d \n", c1, c1); printf("%c - %d \n", c2, c2); return 0;}
字符串表达式
- sizeof
#include "stdio.h"#include "string.h"int main (int args, char* argv[]){ char str[]="JulyEdu"; printf("%d \n", sizeof(str)); printf("%d \n", strlen(str)); return 0;}
- 字符串的修改
#include "stdio.h"#include "string.h"int main (int args, char* argv[]){ char str[]="JulyEdu"; str[4]='a'; str[5]='p'; str[6]='p'; printf("%s \n",str); return 0;}//#include "stdio.h"#include "string.h"int main (int args, char* argv[]){ char str[]="JulyEdu"; *(str+4)='a'; *(str+5)='p'; *(str+6)='p'; printf("%s \n",str); return 0;}//错误:因为“JulyEdu”是字符串常量,不可以修改
//标准写法#include "stdio.h"#include "string.h"int main (int args, char* argv[]){ const char *str="JulyEdu"; //添加const标注 printf("%s \n",str); return 0;}
//指针修改常量字符串#include<iostream>#include <exception>#include "string.h"using namespace std;#define FALSE -1int main (int args, char* argv[]){ const char* strOrigin="JulyEdu"; const int sizeNum=100; try{ char *str=new char[sizeNum]; strcpy(str,strOrigin); cout<<str<<endl; str[4]='a'; str[5]='p'; str[6]='p'; cout<<str<<endl; delete []str; }catch (const bad_alloc& e){ return -1; } return 0;}
//c语言版的字符串修改#include<iostream>#include <exception>#include "string.h"#include<math.h>using namespace std;#define FALSE -1#define ElemType charint main (int args, char* argv[]){ const char* strOrigin="JulyEdu"; const int sizeNum=100; char *str= (char*)malloc(sizeNum*sizeof(ElemType)); if(!str) //分配存储失败 exit(OVERFLOW); strcpy(str,"JulyEdu"); str[4]='a'; str[5]='p'; str[6]='p'; printf("%s\n",str); free(str); return 0;}
凯撒加密
#include<iostream>#include <exception>#include<math.h>#include<fstream>using namespace std;int main(int args, char* argv[]){ ifstream iFile("D://caesar.txt"); if (!iFile.is_open()){ //文件打开错误 cerr << "i_file open error!" << endl; exit(0); } iFile.seekg(0,ios::end); //读取文件的结尾 int nFileLen=iFile.tellg(); //文件的长度 iFile.seekg(0, ios::beg); try{ char *str=new char[nFileLen+1]; iFile.read(str, nFileLen); iFile.close(); str[nFileLen]=0; //将数组最后一位设为0 const int nCaesar=3; int n=0; //n记录回车的数量 for(int i=0; i<nFileLen; i++){ if((str[i]>='a') && (str[i]<='z')){ str[i] += nCaesar; if(str[i] > 'z') str[i] -= 26; } else if((str[i] >= 'A') && (str[i]<='Z')){ str[i] += nCaesar; if(str[i]>'Z') str[i] -= 26; } else if(str[i] == '\n'){ n++; } } ofstream oFile("D://myCaesar.txt"); oFile.write(str, nFileLen-n); oFile.close(); delete[] str; return 0; }catch (const bad_alloc& e){ return -1; }}
异或加密
#include <stdio.h>#include <stdlib.h>#include<math.h>int main(int args, char* argv[]){ FILE* pFile=fopen("D://caesar.txt"); if(pFile==NULL){ exit(0); } fseek(pFile,0,SEEK_END); char str*=(char*)malloc((nFileLen+1)*sizeof(char)); if(!str) //分配存储失败 exit(OVERFLOW); fread(str,nFileLen,1,pFile); fclose(pFile); str[nFileLen]=0; const char*="JulyEdu"; int nLen=(int)strlen(secreat); int j=0; for(int i=0;i<nFileLen;i++){ str[i]^=secret[j]; j++; if(j>=nLen) j-=nLen; } FILE *pFile2=fopen("D://mySong.txt","wb"); fwrite(str,nFile,1,pFile2); fclose(pFile2); delete[] str;}
删除空格
/*删除空格1. main函数:输入字符;异常处理;调用函数;输出函数2. 功能函数:removeBlank*/#include <iostream>using namespace std;void removeBlank(char *pString){ int j=0; for(int i=0;pString[i]!='\0';i++){ if(pString[i] != ' '){ if(i!=j){ pString[j]=pString[i]; } j++; } } pString[j]=0;}int main(int args, char* argv[]){ char str[]= "I have a dream"; removeBlank(str); cout<< str << endl; return 0;}
- 指针版本
//dest:目标;ret:返回#include <iostream>#include <string.h>using namespace std;void removeBlank(char *pString){ char *ret,*dest; //双指针:ret=return;dest=目标 for(ret=dest=pString;*ret!='\0';){ if(*ret==' ') ret++; else *dest++=*ret++; } *ret='\0';}int main(int args, char* argv[]){ char str[]= "I have a dream"; removeBlank(str); cout<< str << endl; return 0;}
- stl 版本
#include<iostream>#include <string>#include <algorithm>using namespace std;void main(){ string str("I have a dream"); //初始化 string::iterator it = str.begin(); while ((it=find(it, str.end(), ' '))!=str.end()) { str.erase(it); if (it == str.end()) break; } cout << str<<endl;}
数组的最大数
#include<iostream>using namespace std;int Select(const int *a, int size);int main(int args,char* argv[]){ const int a[]={1,3,5,2,5,32,15}; int nMax=Select(a,sizeof(a)/sizeof(int)); cout<< nMax <<endl;}int Select(const int* a, int size){ int nMax=a[0]; for(int i=1;i<size;i++){ if(nMax < a[i]) nMax=a[i]; } return nMax;}
数组中最大的两个数
#include<iostream>using namespace std;#defien ERROR 0;void Select(const int* a, int size,int &nMax, int &nSecondMax);int main(int args,char* argv[]){ const int a[]={1,3,5,2,5,32,15}; int nMax, nSecondMax; Select(a,sizeof(a)/sizeof(int),nMax,nSecondMax); cout<< nMax <<endl; cout<< nSecondMax<<endl;}void Select(const int* a, int size,int &nMax, int &nSecondMax){ nMax=a[0]; nSecondMax=a[0]; if(size==0) return ERROR; for(int i=1; i<size;i++){ if(nMax<a[i]){ nSecondMax=nMax; nMax=a[i]; } else if (nSecondMax < a[i]){ nSecondMax=a[i]; } }}
0 0
- 算法初级_1:字符
- 推荐算法:协调过滤初级_1:简介
- Java初级笔记_1
- 算法学习之旅,初级篇(6)--统计字符
- Java初级笔记_IO流_1
- 算法笔试题_1
- 算法_1: 数论
- 初级字符设备驱动
- 每天来点算法_1
- 算法_1: 数论_续
- FFT算法的实现_1
- 排序算法_1,冒泡排序
- 算法竞赛入门经典_1
- Flex与Flash的交互_1(初级简单示例)
- php【初级篇】之_1.PHP集成开发环境
- Android开发初级00_1有关intent的知识点
- 【算法和数据结构】_1_排序算法_1
- 华为OJ 初级:字符逆序
- Android快速开发系列 10个常用工具类
- Leaflet.js+d3
- HDU 2016数据的交换输出
- HDU 2586 How far away ?【LCA】
- 栈 括号匹配程序
- 算法初级_1:字符
- 零碎笔记——Git的简易使用
- 10.related product , up-sell product and cross-sells
- 242. Valid Anagram
- TimerWork
- KVC 与 KVO详解
- 4 C语言程序初体验 实践项目
- 关于gitlab、github的使用
- Cookie中文乱码问题