2024.2.8

本文展示了C语言中的字符串处理函数,包括逆转整个字符串和单词,以及使用bash脚本判断文件类型。
摘要由CSDN通过智能技术生成

 

 

#include<stdio.h>
#include<string.h>
int main(int argc, const char *argv[])
{
	char str[300];
	puts("please input str:");
	gets(str);
	char *p=str;
	int number=0;
	while(*p!='\0')
	{
		if(*p==' ')
		{
			number++;
		}
		p++;
	}
	if(str[0]=='\0')
	{
		printf("str have %d number words:\n",number);
	}
	else
	{
	printf("str have %d number words:\n",number+1);
	}
	return 0;
}

 

 

#!/bin/bash
read file
if [   -b $file ]
then
	echo "块设备"
elif [ -c $file ]
then 
	echo "字符"
elif [ -d $file ]
then
	echo "目录"
elif [ -f $file ]
then 
	echo "普通"
elif [ -L $file ]
then
	echo "软连接"
elif [ -p $file ]
then 
	echo "管道"
elif [ -S $file ]
then
	echo "套接字"
fi

#include<stdio.h>
#include<string.h>
void nizhi(char *str1)
{
	int j=strlen(str1)-1;
	int i=0;
	while(i<j)//整个字符串逆转
	{
		char t=*(str1+i);
		*(str1+i)=*(str1+j);
		*(str1+j)=t;
		i++;
		j--;
	}
	i=j=0;//开始进行字符串内的单词逆转
	while(*(str1+i)!='\0') 
	{
		
		while(*(str1+j)!=' '&&*(str1+j)!='\0') 
		{
			j++;
		}
		int k=j-1;
		while(i<k)//单词开始互换
		{
			char t=*(str1+i);
			*(str1+i)=*(str1+k);
			*(str1+k)=t;
			i++;
			k--;
		}
		while(*(str1+j)==' ')//跳过单词之间的空格
		{
			j++;
		}
		i=j;//下一个开始位置从空格后的单词开始
	}
 
}
void del(char *str2)
{
   char *p=str2,*q=str2;
   while(*str2!='\0')
   {
	   if(*str2==' ')
	   {
		   str2++;
	   }
	   else
	   {
		   *p=*str2;
		   p++;
		   str2++;
	   }
   }
   *p='\0';
   puts(q);
  }


int main(int argc, const char *argv[])
{
	char str[30]="";
	gets(str); 
    nizhi(str);
	del(str);
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值