11、求子字符串”cdf”在父串中出现的次数“11cfbks34cdf2ghacdf11”
#include <stdio.h>
#include<string.h>
int main(int argc, const char * argv[]) {
char str[40] = "11cfbks34cdf2ghacdf11";
char str1[40] = "cdf";
int count = 0;
int i = 0, j = 0;
while(i < strlen(str))
{
if(str1[j] == str[i])
{
if(j == (int)strlen(str1)- 1 )
count++;
else
j++;
}
else
{
j = 0;
}
i++;
}
printf("子串cdf在父串中出现的次数为:%d次\n",count);
return 0;
}
10、自定义函数判断字符串是否是回文? 如:asdfdsa是回文、asds不是回文
*#include <stdio.h>*
*#include<string.h>*
int main(int argc, const char * argv[]) {
char str[40];
scanf("%s",str);
int i = 0;
int len = (int)strlen(str);
while(i < len)
{
if(str[i] == str[len-1])
{
i++;
len--;
}
else
{
printf("不是回文!\n");
break;
}
}
if(i >= len)
printf("是回文!\n");
return 0;
}
//8、自定义函数连接字符串?
*#include <stdio.h>
*#include<string.h>**
char* conn(char *dest,const char *src);
int main(int argc, const char * argv[]) {
char *str = "world";
char dest[40] = "hello ";
conn(dest,str);
printf("连接后的字符串为:%s\n",dest);
return 0;
}
char* conn(char *dest,const char *src)
{
char *add = dest+strlen(dest);
while((*add++ = *src++) != '\0');
return dest;
}