#include <stdio.h> #include <string.h> int main(void) { //char dest[10]; //#define NULL (void *)0 //char *dest = NULL; //dest是野指针,指向的区域没有可读写空间 //char dest[3]; //char src[] = "hello"; char str1[10] = "hello"; //char str2[10] = str1; strcpy(str2, str1); printf("%s ", str2); //printf("%s ", strcpy(dest, src)); //printf("%s ", strncpy(dest, src, sizeof(dest))); return 0; }
#include <stdio.h> #include <string.h> hello helloworld strncmp("hello", "helloworld", 5); int main(void) { int i; char name[][10] = {"xiaoming", "itcast", "xiaoqiang", "xiaohong"}; char key[10]; /* char a[10] = "xiaoming"; //name[0] == > a char b[10] = "itcast"; char c[10] = "xiaoqiang"; char d[10] = "xiaohong"; */ //char name[][10] = {"xiaoming itcast xiaoqiang xiaohong "}; for (i = 0; i < 4; i++) { if (strcmp(name[i], "xiaohong") == 0) { printf("%d %s ", i, name[i]); strcpy(key, name[i]); } } printf("%s ", key); return 0; }
#include <stdio.h> #include <string.h> int main(void) { //char s1[10] = "hello"; //char *s1 = "hello"; char s1[7] = "hello"; //注意S1要有足够的空间放拼接后的字符串,不然发生数组越界 char s2[] = "BOY"; strcat(s1, s2); printf("%s ", s1); return 0; }
#include <stdio.h> #include <string.h> int main(int argc, char *argv[]) { char str[] = "hello world itcast"; char key; printf("%s ", str); key = getchar(); char *p = NULL; p = strchr(str, key); (!p)?printf("un find "):printf("%s ", p); return 0; }
#include <stdio.h> #include <string.h> /* int main(void) { char str[] = "hello world itcast good"; char *p; p = strtok(str, " "); printf("%s ", p); while ((p = strtok(NULL, " ")) != NULL) printf("%s ", p); // p = strtok(str, " "); //printf("str = %s ", str); return 0; } */ /* int main(void) { char str[] = "hello, world, itcast! good.; night?"; char *dlim = " ,?!.;"; //char dlim[] = ".?!.;"; char *p; p = strtok(str, dlim); printf("%s ", p); while ((p = strtok(NULL, dlim)) != NULL) printf("%s ", p); // p = strtok(str, " "); //printf("str = %s ", str); return 0; } */ size_t strlen(const char *str) { return 6; } int main(void) { char str[] = "hello world itcast good night"; char tmp[] = "I am a student "; char *dlim = " "; char *p; printf("%d ", (int)strlen(tmp)); p = strtok(str, dlim); //内部指针保存下一次要strtok的字符串地址 printf("%s ", p); p = strtok(tmp, dlim); printf("%s ", p); while ((p = strtok(NULL, dlim)) != NULL) printf("%s ", p); return 0; }
#include <stdio.h> int a = 3; int b; int main(void) { int c = 3; char *str1 = "hello"; //"hello"属于字符串常量,存储在只读数据区 char str2[] = "hello"; //str2[6] 内存的栈上分配,栈的属性可读可写 printf("%p ", &a); printf("%p ", &b); printf("%p ", &c); printf("%p ", str1); printf("%p ", &str1); printf("%p ", str2); //str2+1 printf("%p ", &str2);// &str2+1 return 0; }