step1 . day8 C语言基础练习之指针和函数

今天继续复习指针,还是很深奥的,两点注意事项:

1. int型数据可以强制类型转化赋值给指针变量,然后对该地址赋值(用在裸机上);

2.指针数组是数组,存放的是指针,数组指针是数组的指针,存放的是行指针。

利用数组指针对输入数组进行单词数目确认的函数:

#include <stdio.h>

int wordcount(char *str){
int count;
int word = 0;
while(*str){
count = 0;
while((*str>='a' && *str<='z') || (*str>='A' && *str<='Z')){
count++;
str++;
}
if(*str == ' '){
if(count>2) word++;
str++;
continue;
}
else if(*str == '\0')
{
if(count>2) word++;
break;
}
else {
do{
str++;

}while(*str != ' ' && *str!= '\0');
}
}
return word;

}
int main(int argc, const char *argv[])
{
char str[100];
printf("please input a str:");
scanf("%[^\n]",str);

int ret = -1;

ret = wordcount(str);

printf("word in str:%d\n",ret);
return 0;
}

使用指针和二级指针对数组进行单词翻转的函数

#include <stdio.h>
#include <string.h>

void swap(char **h,char **t){

while(*t > *h){
**t ^= **h;
**h ^= **t;
**t ^= **h;
(*h)++;
(*t)--;
}
}

void strchange(char *str){
char *head;
char *tail;
head = str;
tail = str + strlen(str)-1;

swap(&head,&tail);

tail = str;
head = str;
char *temp;
while(1){
while(*tail != ' ' && *tail)
tail++;
temp = (--tail);
swap(&head,&tail);
if(*(tail+1)=='\0') break;
head = temp+2;
tail = temp+2;
}
}


int main(int argc, const char *argv[])
{
char str[100]={0};
printf("please input a str:");

scanf("%[^\n]",str);

strchange(str);

printf("str revese:%s\n",str);
return 0;
}

 

转载于:https://www.cnblogs.com/huiji12321/p/11135006.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值