标签:str1 ase cstring mic war 实现 icm rup std
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string.h> //<string.h>是C版本的头文件,包含比如strcpy、strcat之类的字符串处理函数。 //<cstring>是C++版本的<string.h> //<string>定义了一个string的字符串类,包含了string类的各种操作,如s.size(), s.erase(), s.insert()等。 //但<string>又包含了老的C版本的字符串操作如strcpy、strcat等 void main() { char str[30] = "yincheng"; char str1[20] = "8848.88"; strncat(str, str1, 4);//从str1中拷贝4个字节到str printf("%s\n", str); system("pause"); }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> void mystrncat(char *bc, char *c, int length) { if (bc == NULL || c == NULL || length == 0) return; char *p = bc; while (*p) p++; //循环结束后,p指向了字符串的末端‘\0‘ for (int i = 0; i < length; i++) { *p = c[i]; p++; } *p = ‘\0‘; } void main() { char str[30] = "task"; char str1[20] = "list8848.88"; mystrncat(str, str1, 4); printf("%s\n", str); system(str); system("pause"); }
#include<stdio.h> #include<stdlib.h> void main() { char str[10] = "88e48"; //int num = atoi(str);//8848 //int num = atoi(str+1);//848 int num = atoi(str);//8848 char s1[10] = "88e48"; //int n1 = atoi(s1);//88 int n1 = atoi(s1+3);//48 char s2[10] = "e8848"; //int n2 = atoi(s2);//0 int n2 = atoi(s2 + 1);//8848 printf("%d\n", n2); system("pause"); }
#include<stdio.h> #include<stdlib.h> #include<string.h> void mystrrev(char *p) { int length = strlen(p);//获取字符串长度 for (int i = 0; i < length/2; i++)//length必须除以2,不然字符对调之后又调回去了 { char temp = p[i]; p[i] = p[length - 1 - i]; p[length - 1 - i] = temp; } } void main() { char str[20] = "yincheng8848a"; printf("原来字符串 %s\n", str); //_strrev(str); mystrrev(str); printf("后来字符串 %s\n", str); system("pause"); }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string.h> void tobig(char *p) { while (*p) { if ((*p)>= ‘a‘&&(*p) <= ‘z‘) *p -= 32; p++; } } void main() { char str[20] = "notepad"; //_strupr(str);//小写转大写 //toupper用法; #include<cctype> ch = toupper(ch);//ch是一个字符 tobig(str); printf("%s\n", str); char str1[20] = "TASKLIST"; _strlwr(str1);//大写转小写 printf("%s\n", str1); system("pause"); }
#include<stdio.h> #include<stdlib.h> #include<string.h> int mystrlen(char *p) { int i = 0; while (*p) { i++; p++; } return i; } void main() { char str[100] = "i love iphone";//str是变量 char *p = "i love china";//常量 int len1 = strlen(str); int len2 = mystrlen(p); printf("str=%d,p=%d\n",len1,len2); system("pause"); }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string.h> char * mystrcpy(char *dst,char *src) { char *p = dst; if (dst == NULL || src == NULL) return NULL; while (*src) { *dst = *src; dst++; src++; } *dst = ‘\0‘; return p; } char * mystrcat(char *dst, char *src) { char *p = dst; if (dst == NULL || src == NULL) return NULL; while (*dst) dst++; while (*src) { *dst = *src; dst++; src++; } *dst = ‘\0‘; return p; } void main() { char str1[10] = "note"; char str2[10] = "pad"; char str[20]; printf("%s\n", mystrcpy(str, str1)); //mystrcpy(str, str1);//复制 printf("%s\n", str); //strcat(str, str2);//拼接 mystrcat(str, str2);//拼接 printf("%s\n", str); system(str); } void main0() { char str1[10] = "note"; char str2[10] = "pad"; char str[20]; sprintf(str, "%s%s", str1, str2); system(str); system("pause"); }
#include<stdio.h> #include<stdlib.h> #include<string.h> char * mystrchr(char *dst,char ch) { if (dst == NULL) return NULL; while (*dst) { if (*dst == ch) return dst; dst++; } return NULL; } void main() { char str[20] = "i love china"; char ch=‘o‘; /*char *p = strchr(str, ch);*/ char *p = mystrchr(str, ch); if(p==NULL) printf("没找到\n"); else printf("值%c,地址%x\n",*p,p); system("pause"); }
#include<stdio.h> #include<stdlib.h> #include<string.h> int mystrcmp(char *p1,char *p2) { int l1 = strlen(p1); int l2 = strlen(p2); if (l1 != l2) return -1; else { for (;*p1;p1++,p2++) { if (*p1 != *p2) return -1; } } return 0; } void main() { char str1[10] = "note"; char str2[10] = "note"; /*if (strcmp(str1,str2)==0)*/ if (mystrcmp(str1, str2) == 0) printf("相等\n"); else printf("不相等\n"); system("pause"); } void main0() {//无论相等还是不等,都输出不等 char str1[10] = "note"; char str2[10] = "note"; if(str1==str2)//c语言判断字符串不能这样 printf("相等\n"); else printf("不相等\n"); system("pause"); }
#include<stdio.h> #include<stdlib.h> #include<string.h> char * mystrstr(char *allstr,char *str) { if (allstr == NULL || str == NULL) return NULL; int all_len = strlen(allstr); int str_len = strlen(str); for (int i = 0; i <all_len-str_len+1; i++) { int flag = 1;//标示,假定字符串一开始相等 for (int j = 0; j <str_len; j++) { if (allstr[i + j] != str[j]) { flag = 0; break; } } if (flag) return allstr+i; //返回找到的地址 } return NULL; } void main() { char allstr[40] = "i love china i love cpp"; char str[10] = "china"; //char *p = strstr(allstr, str);//检索 char *p = mystrstr(allstr, str);//检索 if(p==NULL) printf("没找到\n"); else printf("值%c,地址%x\n",*p,p); system("pause"); }
#include<stdio.h> #include<stdlib.h> #include<string> #include<memory.h>//内存操作函数 void main() { char str[40] = "i love china i love c i love cpp"; //第一个参数是内存的首地址,第二个参数是要设置的字符 //第三个参数是整数,从首地址开始前进多少字节,把这一段设置为该字符 memset(str, ‘A‘, 10); printf("%s\n",str); memset(str, ‘A‘, 12); printf("%s\n", str); memset(str, ‘\0‘,strlen(str));//清空字符串 printf("%s\n", str); system("pause"); }
#include<stdio.h> #include<stdlib.h> #include<string> #include<memory.h>//内存操作函数 void main0() { char str[30] = "yincheng8848"; char str1[20] = "hello c"; printf("%s\n", str); //memcpy:从地址str开始,从str1处拷贝5个字节到str memcpy(str,str1,5); printf("%s\n",str); system("pause"); } void main() { int a[] = { 1,3,5,7,9 }; int b[] = { 0,2,4,6,8 }; memcpy(a,b, 8);//每个元素4个字节,两个元素8个字节 //memcpy按照内存字节来拷贝,不管是什么类型,都是拷贝二进制类型 for (int i = 0; i < 5; i++) { printf("%d\n",a[i]); } system("pause"); }
#include<stdio.h> #include<stdlib.h> #include<string> #include<memory.h>//内存操作函数 void main() { char str[30] = "yincheng8848"; char str1[20] = "hello c"; printf("%s\n", str); _memccpy(str,str1,‘l‘,7); //memccpy:如果str1没有字符‘l’,就等价于memcpy,读取7个字符拷贝到str //如果有,读取拷贝到该字符就停止 printf("%s\n",str); system("pause"); }
#include<stdio.h> #include<memory.h> void main() { char str[30] = "helloyincheng"; char ch=‘c‘; char *p =(char *) memchr(str, ‘c‘, 30); //从地址str开始,前进30个字节检索 //如果存在,返回地址,否则返回空 if (p == NULL) printf("没找到\n"); else printf("找到字符%c,地址%p", *p, p); getchar(); }
#include<stdio.h> #include<stdlib.h> #include<memory.h> void main() { char *buf1 = "ABCDE123"; char *buf2 = "abcde456"; int num = _memicmp(buf1, buf2, 5); //memicmp判断字符串前面几个字节是否相等 //忽略大小写,相等返回0,反之非0 if (num == 0) printf("忽略大小写的情况下,相等"); else printf("不等"); getchar(); }
[c/c++] programming之路(23)、字符串(四)——strncat,atoi,strcmp,strlen等,以及常用内存函数
标签:str1 ase cstring mic war 实现 icm rup std
原文地址:http://www.cnblogs.com/little-monkey/p/7526509.html