C语言刷题
用指针实现删除字符串中出现的指定字符。(字符串和指定字符均由键盘输入)
#include <stdio.h>
void delChar(char* str, char c)
{
int i = 1;
char* p1 = str;
char* p = str;
while (*p != 0)
{
if (*p == c)
{
for (p1 = p, i = 1; *p1 != 0; p1++)
{
for (; 1; i++)
{
if (*(p1 + i) != c)
{
*p1 = *(p1 + i);
break;
}
}
}
}
p++;
}
}
int main()
{
char str[100] = { 0 };
gets(str);
char c;
scanf("%c", &c);
delChar(str, c);
puts(str);
return 0;
}
利用指针编写程序实现3*3矩阵的转置
#include<stdio.h>
int main()
{
void transpose(int (*p)[3],int (*q)[3]);
int a[][3]={1,2,3,
4,5,6,
7,8,9};
int b[][3]={1,2,3,
4,5,6,
7,8,9};
int (*p)[3]=a;
int (*q)[3]=b;
transpose(p,q);
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
printf("%d ",b[i][j]);
}
printf("\n");
}
}
void transpose(int (*p)[3],int (*q)[3])
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
q[j][i]=p[i][j];
}
}
输入一个字符串,当字符串长度为奇数时最中间的字符不动,左右半边平移互换位置。例如输入abcdefg则输出efgdabc;当字符串长度为偶数时,左右半边直接平移互换位置,例如:输入abcdefgh则输出efghabcd。请编写swap(char *a,int n)函数实现该功能。
#include<stdio.h>
#include<string.h>
int main()
{
int n;
void swap(char* a, int n);
char c[100] = { '0' };
gets(c);
char* a = c;
n = strlen(c);
swap(a, n);
puts(c);
return 0;
}
void swap(char* a, int n)
{
char temp;
if (n % 2 == 0)
{
for (int i = n / 2; i < n; i++)
{
temp = a[i];
a[i] = a[i - n / 2];
a[i - n / 2] = temp;
}
}
else
{
for (int i = (n + 1) / 2; i < n; i++)
{
temp = a[i];
a[i] = a[i - (n + 1) / 2];
a[i - (n + 1) / 2] = temp;
}
}
}