1、逆序
#include <stdio.h>
#include <string.h>
void inr(char a[],int n);
int main()
{
char arr[50];
gets(arr);
int n,i;
i=strlen(arr);
// puts(arr);
// for(n=0;n<i;n++)
// {
// printf("%c",arr[n]);
// }
inr(arr,i);
puts(arr);
// printf("\n");
// for(n=0;n<i;n++)
// {
// printf("%c",arr[n]);
// }
return 0;
}
void inr(char a[],int n)
{
int i;
char tp;
for(i=0;i<n/2;i++)
{
tp=a[i];
a[i]=a[n-i-1];
a[n-i-1]=tp;
}
}
结果
0123456789
9876543210
--------------------------------
Process exited after 2.465 seconds with return value 0
请按任意键继续. . .
2、编写一个程序,功能是读取输入,直到遇到换行, 并报告单词数,大写字母
数,小写字母数,标点符号数和数字字符数,使用ctype. h系列函数
#include <stdio.h>
#include <ctype.h>
/*
2.编写一个程序,功能是读取输入,直到遇到换行, 并报告单词数,大写字母
数,小写字母数,标点符号数和数字字符数,使用ctype. h系列函数
*/
int main()
{
char arr[100]={0};
fgets(arr,100,stdin);
// char *ch;
// ch=arr;
int i=0,j=0,num_A=0,num_a=0,num_1=0,num_0=0,ret_a,ret_A,ret_1;
while(*(arr+i)!='\n')
{
if((*(arr+i)>='a'&&*(arr+i)<='z')||(*(arr+i)>='A'&&*(arr+i)<='Z'))//判断字母
{
if(j==0)
j++;
else
{
if((*(arr+i-1)>='a'&&*(arr+i-1)<='z')||
(*(arr+i-1)>='A'&&*(arr+i-1)<='Z'))//判断连续
;
else
j++;
}
}
ret_a=islower(*(arr+i));//是否为小写字母
if(ret_a==2)
num_a++;
ret_A=isupper(*(arr+i));//是否为大写字母
if(ret_A==1)
num_A++;
ret_1=isdigit(*(arr+i));//是否为数字
if(ret_1==1)
num_1++;
if(ret_a!=2&&ret_A!=1&&ret_1!=1)
num_0++;
i++;
}
printf("单词数为%d\n",j);
printf("小写字母数为%d\n",num_a);
printf("大写字母数为%d\n",num_A);
printf("数字数为%d\n",num_1);
printf("标点符号数为%d\n",num_0);
return 0;
}
结果
zxcsASDF,,,5
单词数为1
小写字母数为4
大写字母数为4
数字数为1
标点符号数为3
--------------------------------
Process exited after 10.14 seconds with return value 0
请按任意键继续. . .
3、编写一个程序,按照相反的单词顺序显示命令行参数,即,如果命令行参数
是see you later,程序的显示应为later you see
#include <stdio.h>
#include <string.h>
/*
3.编写一个程序,按照相反的单词顺序显示命令行参数,即,如果命令行参数
是see you later,程序的显示应为later you see
*/
int main()
{
char arr[50]={0};
char *num[20]={0};
char *ch;
int i=0;
fgets(arr,50,stdin);
// puts(arr);
ch=strtok(arr," ");
num[i]=ch;
// printf("%s ",num);
// puts(num);
while(ch!=NULL)
{
i++;
ch=strtok(NULL," ");
num[i]=ch;
}
// puts(num);
for(i=i-1;i>-1;i--)
{
printf("%s ",*(num+i));
// num--;
}
return 0;
}
结果
shan you mu xi mu you zhi
zhi
you mu xi mu you shan
--------------------------------
Process exited after 13.59 seconds with return value 0
请按任意键继续. . .
有瑕疵
4、编写一个函数string_ in(), 它接受两个字符串指针参数,如果第二个字符
串被包含在第一个字符串中,函数就返回被包含的字符串开始的地址,例如:
string_ in( “hats”,”at”)返回hats中a的地址,否则,函数返回空指针,
在一个使用循环语句为这个函数提供输入的完整程序中进行测试
(不要使用库函数)
#include <stdio.h>
int fun(char *p,char *o);
/*
4.编写一个函数string_ in(), 它接受两个字符串指针参数,如果第二个字符
串被包含在第一个字符串中,函数就返回被包含的字符串开始的地址,例如:
string_ in( “hats”,”at”)返回hats中a的地址,否则,函数返回空指针,
在一个使用循环语句为这个函数提供输入的完整程序中进行测试
(不要使用库函数)
*/
int main()
{
char num_1[50]={0},num_2[10]={0};
printf("num_1=");
fgets(num_1,50,stdin);
printf("num_2=");
fgets(num_2,10,stdin);
int ret;
ret=fun(num_1,num_2);
if(ret==0)
printf("空");
else
printf("%d",ret);
return 0;
}
int fun(char *p,char *o)
{
int i=0,j=0,ret;
for(;*(o+i)!='\n';j++)
{
if(*(p+j)==*(o))
ret=j;
for(;*(p+j)==*(o+i);)
{
// const ret=j;
j++;
i++;
if(*(o+i)=='\n')
return ret;
else
;
}
if(j>50)
break;
}
return 0;
}
结果
num_1=asdffgh
num_2=fg
3
--------------------------------
Process exited after 8.936 seconds with return value 0
请按任意键继续. . .
5、实现删除-一个字符串中的指定字母,如:字符串“abcd”,删除其中的”a”字母,剩余”bcd”,也可
以传递多个需要删除的字符,传递”ab”也可以做到删除”ab”,剩余”cd”。
#include <stdio.h>
#include <string.h>
/*
5.实现删除-一个字符串中的指定字母,如:字符串“abcd”,删除其中的”a”字母,剩余”bcd”,也可
以传递多个需要删除的字符,传递”ab”也可以做到删除”ab”,剩余”cd”。
*/
int main()
{
char arr[50]={0};
printf("请输入一串字符:");
fgets(arr,50,stdin);
char num[20]={0};
printf("请输入要删除的字符:");
fgets(num,20,stdin);
int i,j;
for(i=0;*(num+i)!='\n';i++)
{
j=0;
for(j=0;*(arr+j)!='\n';j++)
{
if(*(arr+j)==*(num+i))
*(arr+j)=0;
}
}
for(j=0;*(arr+j)!='\n';j++)
{
if(*(arr+j)!=0)
printf("%c",*(arr+j));
}
return 0;
}
结果
请输入一串字符:aasscdfdg
请输入要删除的字符:sd
aacfg
--------------------------------
Process exited after 15.37 seconds with return value 0
请按任意键继续. . .