1、删除子串,只要是原串中有相同的子串就删掉,不管有多少个,返回子串个数。
#include <stdio.h>
//不能对指针或者常量字符串用sizeof求长度;常量转化为非常量字符串,需要强制类型转换;sizeof求的字符串长度是包括'\0'这个隐藏元素的;
int delete_sub_str(const char *str,const char *sub_str,char *result)
{
int count = 0 ;
char *p = NULL,*subp = NULL;
p = (char *)str;
subp = (char *)sub_str;
int c1 = 0,c2 = 0;
while (*p != '\0')
{
c1++;
p++;
}
while (*subp != '\0')
{
c2++;
subp++;
}
printf("orign string is %s , the length is %d \n",str,c1);
printf("sub_string is %s, the length is %d \n",sub_str,c2);
int j;
p = (char *)str;
subp = (char *)sub_str;
for (int i = 0 ,jj = 0 ; i < c1 ; )
{
if ( *p == *subp )
{
for ( j = 1 ; j < c2 ; j++)
{
if (*(p+j) == *(subp+j))
;
else
break;
}
if ( j == c2)
{
count ++;
i += 3;
p = p+3;
}
else
{
result[jj] = *p;
jj++;
i++;
p++;
}
}
else
{
result[jj] = *p;
jj++;
i++;
p++;
}
}
result[100] = '\0';
return count;
}
void main()
{
char s[100] = "\0";
int num = delete_sub_str("sabc123123123de123123de123cdeadbce123de","abc",s);
printf("the number of sub_string is %d \n",num);
printf("the rest string is %s \n",s);
}
2、约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。
#include <STDIO.H>
#include <STDLIB.H>
typedef struct Node
{
int password;
int num;
struct Node *next;
}Node,*Link;
void InitList(Link &L)//创建了一个头节点
{
L = (Node *)malloc(sizeof(Node));
if (!L) exit(1);
L->password = 0;
L->num = 0;
L->next = L;
}
void Create(int n,Link &L)
{
Link p,q;
q = L;
for (int i=1;i<=n;i++)
{
p = (Node *)malloc(sizeof(Node));
if(!p) exit(1);
printf("Please input the %d th person's password : ",i);
scanf("%d",&p->password);
p->num = i;
L->next = p;
L =p;
}
L->next = q->next;
free(q);
}
void main()
{
Link L,p,q;
int n,m;
int a = 1;
int b = 1;
int k = 1;
while (b == 1)
{
printf("*************************** The %d th Josephus circu ************************\n",k);
//L = NULL;
InitList(L);
printf("Please input the total number of people N : ");
scanf("%d",&n);
while (n<=1)
{
printf("Your number is noncorrect,please input a number which is larger than 1: ");
scanf("%d",&n);
}
printf("Please input the total first code M : ");
scanf("%d",&m);
while (m<=1)
{
printf("Your max is noncorrect,please input a number which is larger than 1: ");
scanf("%d",&m);
}
Create(n,L);
printf("The final death order is : \n");
p = L;
for (int i=1;i<=n;i++)
{
for (int j=1;j<m;j++)
{
p = p->next;
}
q = p->next;
m = q->password;
printf("%d ",q->num);
p->next = q->next;
free(q);
}
printf("\n*************************** The %d th Josephus circu ************************\n\n",k);
k++;
printf("Continue?(press '1' to proceed and '0' to exit):\n");
scanf("%d",&b);
}
}
3、比较一个数组的元素 是否为回文数组.
#include <stdio.h>
#define MAXNUM 100
bool huiwen(char* num,int count)
{
for (int i = 0 ; i < count/2 ; i++)
{
if(!((num[i] == num[count-1-i])?true:false))
{
break;
}
}
if (i == count/2)
{
return true;
}
else
{
return false;
}
}
void main()
{
printf("====判断数组元素是否为回文数====\n");
char num[] = "12353211";
printf("数组为%s\n",num);
int count = sizeof(num)/sizeof(char) - 1;
printf("数组元素个数为%d\n",count);
if (huiwen(num,count))
{
printf("数组元素是回文数组!\n");
}
else
printf("数组元素不是回文数组!\n");
}
//main函数还可以这样写。
//好处是:实现用户输入;
//坏处是:使用了strlen;
void main()
{
char str[MAXNUM] = {0};
while(1)
{
printf("请输入一个字符串:");
scanf("%s",str);
int count = strlen(str);
if (huiwen(str,count))
{
printf("数组元素是回文数组!\n");
}
else
printf("数组元素不是回文数组!\n");
}
}
• 问题描述:
比较两个数组,要求从数组最后一个元素开始逐个元素向前比较,如果2个数组长度不等,则只比较较短长度数组个数元素。请编程实现上述比较,并返回比较中发现的不相等元素的个数
比如:
数组{1,3,5}和数组{77,21,1,3,5}按题述要求比较,不相等元素个数为0
数组{1,3,5}和数组{77,21,1,3,5,7}按题述要求比较,不相等元素个数为3
• 要求实现函数:
int array_compare(int len1, int array1[], int len2, int array2[])
【输入】 int len1:输入被比较数组1的元素个数;
int array1[]:输入被比较数组1;
int len2:输入被比较数组2的元素个数;
int array2[]:输入被比较数组2;
【输出】 无
【返回】 不相等元素的个数,类型为int
• 示例
1) 输入:int array1[] = {1,3,5},int len1 = 3,int array2[] = {77,21,1,3,5},int len2 = 5
函数返回:0
2) 输入:int array1[] = {1,3,5},int len1 = 3,int array2[] = {77,21,1,3,5,7},int len2 = 6
函数返回:3