1. Error correction:
idea: array out-of-bounds problem
(a)、
void test1()
{
char string[10];
char *str1 = "0123456789";//超出数组范围了,10个外加上一个\0,11个
strcpy(string, str1);
}
(b)、
void test2()
{
char string[10], str1[10];
for (i=0; i<10; i++)//int i;
{
str1[i] = 'A';
}
strcpy(string, str1);//strcpy此时不能用于数组之间的赋值
}
(c)、
void test3(char *str)//char *str1
{
char string[10];
if (strlen(str1)<=10)
{
strcpy(string, str1);
}
}
2. Find a player: 2 badminton teams compete, each with 3 players, and each player only competes once. Team A consists of A, B, and C, and Team B consists of X, Y, and Z. Someone inquired about the competition list, A said that he did not compare with X, and C said that he did not compare with X and Z. Program to find the list of three teams of players.
Idea: Just follow the conditions
百度得:
#include <stdio.h>
#define a d[0]
#define b d[1]
#define c d[2]
int main(void)
{
char d[]="zyx",temp;
int i=0;
while(!(a!='x'&&c!='x'&&c!='z'))
{
i++;
if(i%2){temp=a;a=b;b=temp;}
if(i%3){temp=b;b=c;c=temp;}
else{temp=a;a=c;c=temp;}
puts(d);
}
printf("a=%c b=%c c=%c\n",a,b,c);
return 0;
}
3. Use C language to replace substrings in strings, return 0 if successful, and -1 if failed. For example, in the string "ABCDEFG", replace "BCD" with the substring "9527", and the result becomes : "A9527EFG"
function prototype:
int str_replace(char str, char replaced_str, char *new_str)
#include<stdio.h>
#include <string.h>
int str_replace(char *str, char * replaced_str, char *new_str)
{
}
int main(void)
{
return 0;
}