您的位置:首页 > 编程语言 > C语言/C++

c语言自定义方法实现字符串的7个操作

2014-08-16 14:58 846 查看
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define N 100

int main(int argc, const char * argv[])

{

//1.求字符数组的长度strlen

char buf
="";

int i;

gets(buf);

for (i=0; *(buf+i); i++);

printf("strlen=%d\n",i);

*/

//2.bezero 函数的实现

/* char buf
="";

int i;

gets(buf);

for (i=0; *(buf+i); i++) {

*(buf+i)=0;

}

printf("buzero=%s\n",buf);

printf("buf 的长度为 %d\n",strlen(buf));

*/

//3.strcat 字符串的拼接

/*

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define N 100

int main(int argc, const char * argv[])

{

char buf1
="";

char buf2
="";

int i;

int j;

printf("请输入第一个字符串:");

gets(buf1);

printf("请输入第二个字符串:");

gets(buf2);

for (i=0;*(buf1+i); i++);

for (j=0;*(buf2+j);j++)

{

// buf1[i+j]=buf2[j];

*(buf1+i+j)=*(buf2+j);

}

printf("拼接后的字符串为buf1=%s\n",buf1);

}

*/

//4.字符串复制 strcpy

/*

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define N 100

int main(int argc, const char * argv[])

{

char buf1
="";

char buf2
="";

int i;

printf("请输入buf2\n");

gets(buf2);

for (i=0;*(buf2+i); i++)

{

*(buf1+i)=*(buf2+i);

}

printf("buf2=%s\n",buf2);

printf("将buf2复制到buf1=%s\n",buf1);

}

*/

//5.字符串比较strcmp

//从第一个不相同的数开始比较

/*

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define N 100

int main(int argc, const char * argv[])

{

char buf1[2*N]="";

char buf2
="";

int i;

gets(buf1);

gets(buf2);

for (i=0; *(buf1+i)&&(buf2+i); i++) {

if ((buf1+i)!=(buf2+i)) {

break;

}

}

printf("(buf1+%d)-(buf2+%d)=%d\n",i,i,*(buf1+i)-*(buf2+i));

}

*/

//6.在字符串中找到字符 strchr

/*

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define N 100

int main(int argc, const char * argv[])

{

char buf1[2*N]="";

char ch;

int i;

gets(buf1);

ch=getchar();

for (i=0; *(buf1+i); i++) {

if(*(buf1+i)==ch)

{

break;

}

}

!(buf1+i)?printf("null\n"):printf("%s\n",buf1+i);

printf("\n");

}

*/

//7.在字符串中找字符串 strstr

/* #include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define N 100

int main(int argc, const char * argv[])

{

char buf1
="";

char buf2
="";

int i;

int j;

gets(buf1);

gets(buf2);

for (i=0; *(buf1+i); i++) {

for(j=0;*(buf2+j);j++)

{

if(*(buf1+i+j)!=*(buf2+j))

{

break;

}

}

if(!(*(buf2+j)))

{

printf("%s\n",buf1+i);

return 0;

}

}

}

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: