strstr函数与翻转字符串
2016-07-25 11:38
393 查看
//模拟实现strstr函数//
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
const char *my_strstr(const char *str, const char *dest)
{
assert(*str != 0);
assert(*dest != 0);
for (int i = 0; str[i] != '\0'; i++)
{
int tmp = i;
int j = 0;
while (str[i++] == dest[j++])
{
if (dest[j] == '\0')
{
return &str[tmp];
}
}
i = tmp;
}
return NULL;
}
int main()
{
char *s = "bit6661234";
char *d = "612";
printf("%s\n", my_strstr(s, d));
printf("%s\n", strstr(s, d));
system("pause");
return 0;
}
//实现一个函数翻转字符串(将一个字符串的内容,逆序存储)//
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
char* my_reverse(char *dest,const int len)
{
int i = 0;
int j = len - 1;
for (i = 0; i < j; i++, j--)
{
dest[i] ^= dest[j];
dest[j] ^= dest[i];
dest[i] ^= dest[j];
}
return dest;
}
int main()
{
char dest[20] = "welcome to bit";
int len = strlen(dest);
printf("%s\n", my_reverse(dest,len));
system("pause");
return 0;
}
//每瓶汽水1元,两个空瓶可以置换一瓶汽水,现在有20元,最多能喝多少瓶汽水。//
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<stdlib.h>
int main()
{
int bottle = 20;
int count = 0;
count = bottle;
while (bottle >= 2)
{
count += bottle / 2;
bottle = bottle / 2 + bottle % 2;
}
printf("喝了%d瓶\n", count);
system("pause");
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
const char *my_strstr(const char *str, const char *dest)
{
assert(*str != 0);
assert(*dest != 0);
for (int i = 0; str[i] != '\0'; i++)
{
int tmp = i;
int j = 0;
while (str[i++] == dest[j++])
{
if (dest[j] == '\0')
{
return &str[tmp];
}
}
i = tmp;
}
return NULL;
}
int main()
{
char *s = "bit6661234";
char *d = "612";
printf("%s\n", my_strstr(s, d));
printf("%s\n", strstr(s, d));
system("pause");
return 0;
}
//实现一个函数翻转字符串(将一个字符串的内容,逆序存储)//
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
char* my_reverse(char *dest,const int len)
{
int i = 0;
int j = len - 1;
for (i = 0; i < j; i++, j--)
{
dest[i] ^= dest[j];
dest[j] ^= dest[i];
dest[i] ^= dest[j];
}
return dest;
}
int main()
{
char dest[20] = "welcome to bit";
int len = strlen(dest);
printf("%s\n", my_reverse(dest,len));
system("pause");
return 0;
}
//每瓶汽水1元,两个空瓶可以置换一瓶汽水,现在有20元,最多能喝多少瓶汽水。//
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<stdlib.h>
int main()
{
int bottle = 20;
int count = 0;
count = bottle;
while (bottle >= 2)
{
count += bottle / 2;
bottle = bottle / 2 + bottle % 2;
}
printf("喝了%d瓶\n", count);
system("pause");
return 0;
}
相关文章推荐
- Android Studio 主题、字体大小的设置
- c语言==复合数据类型(14)
- 奶牛的锻炼
- 嗖嗖嗖
- Genymotion的使用以及错误解决
- web前端开发小工具集合
- Linux查看CPU和内存使用情况【转】
- luigi学习1
- poj 2965---The Pilots Brothers' refrigerator
- PAT 1018 A+B in Hogwarts (20)
- JavaScript跨域总结与解决办法
- iOS errorcode为-1011 问题及解答
- Bundler的安装和配置
- 重磅!8大策略让你对抗机器学习数据集里的不均衡数据
- redis和memcache
- 求二进制数中1的个数
- php根据二维数组中的某个值去除该数组。
- Intellij idea 快键键
- Sql Server随机取数据
- Linux基础命令