C语言程序设计实例26
字符数组s1、s2,把s2在s1中出现的字符全部删除。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char s1[50], s2[50], *p1, *p2;
int i, j;
p1 = s1;
p2 = s2;
printf("s1=");
scanf("%s", p1);
printf("s2=");
scanf("%s", p2);
while (*p2)
{
for (i = 0, j = 0; *(p1 + i) != '\0';i++)
{
if (*(p1 + i) != *p2)
{
*(p1 + (j++)) = *(p1 + i);
}
}
*(p1 + j) = '\0';
p2++;
}
puts(p1);
system("pause");
return 0;
}
C语言程序设计实例27
将b字符串连接到a字符串后面,编写函数实现此功能
#include <stdio.h>
#include <stdlib.h>
int main()
{
char s1[100], s2[50], *p1, *p2;
p1 = s1;
p2 = s2;
printf("s1=");
scanf("%s", p1);
printf("s2=");
scanf("%s", p2);
while (*p1++);
p1--;
while (*p2)
{
*p1++= *p2++;
}
*p1 ='\0';
puts(s1);
system("pause");
return 0;
}
C语言程序设计实例28
将b字符串复制到a中,编写函数实现此功能
#include <stdio.h>
#include <stdlib.h>
int main()
{
char s1[100], s2[50], *p1, *p2;
p1 = s1;
p2 = s2;
printf("s2=");
scanf("%s", p2);
while (*p2)
{
*p1++= *p2++;
}
*p1 ='\0';
puts(s1);
system("pause");
return 0;
}
C语言程序设计实例29
#include <stdio.h>
#include <stdlib.h>
int main()
{
char s1[100], *p1;
int space=0, letters=0, num=0, others=0;
p1 = s1;
printf("s1=");
scanf("%s", p1);
while (*p1)
{
if (*p1 >= 'a'&&*p1 <= 'z' || *p1 >= 'A'&&*p1 <= 'Z')
{
letters++;
}
else if (*p1 >= '0'&&*p1 <= '9')
{
num++;
}
else if (*p1 == ' ')
{
space++;
}
else
{
others++;
}
p1++;
}
printf("\nletters=%d", letters);
printf("\nnum=%d", num);
printf("\nspace=%d", space);
printf("\nothers=%d", others);
system("pause");
return 0;
}
C语言程序设计实例30
打印出如下图形:
*
***
*****
***
*
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x, i, j, n = 3;
for (i = 0; i < 3; i++)
{
for (j = n--; j > 0; j--)
{
printf(" ");
}
for (x = 0; x < 2 * i + 1; x++)
{
printf("*");
}
printf("\n");
}
n = 1;
for (i = 3; i >0; i--)
{
for (j = 0; j <=n; j++)
{
printf(" ");
}
for (x = 2 * (i-1) - 1; x > 0; x--)
{
printf("*");
}
n++;
printf("\n");
}
system("pause");
return 0;
}