关于字符我有话说

‘\0’是数组结束的标志。
输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数

#include<stdio.h>
int main(){
char c;
int letters=0,space=0,digits=0,others=0;
while((c=getchar())!='\n'){
    if(c>='a'&&c<='z'||c>='A'&&c<='Z'){
       letters++;
    }
    else if(c>='0'&&c<='9') digits++;
    else if(c==' ') space++;
    else others++;

}
printf("letters=%d,space=%d,digits=%d,others=%d",letters,space,digits,others);
}

输入一行字符,统计单词个数

#include<stdio.h>
#include<string.h>
/****word=0,表示前面的字符是空格************/
int main(){
char str[100];
char c;
int num=0,word=0;
gets(str);
for(int i=0;(c=str[i])!='\0';i++){//不是回车则循环继续执行
    if(c==' ') word=0;
    else if(word==0){
        word=1;
        num++;
    }
}
printf("%d",num);
return 0;
}

有3 个字符串,要求找出其中最大者

#include<stdio.h>
#include<string.h>
int main()
{
    char a[3][20],string[20];
    int i;
    for(i=0;i<3;i++)
    scanf("%s",a[i]);
    if(strcmp(a[0],a[1])>0) strcpy(string,a[0]);
    else strcpy(string,a[1]);
    if(strcmp(string,a[2])>0) printf("%s",string);
    else printf("%s",a[2]);
    return 0;
}

strcat函数(自写)

int cat(char s1[],char s2[]){
int i,j;
char s[1000];
for(i=0;s1[i]!='\0';i++){
    s[i]=s1[i];
}
for(j=0;s2[j]!='\0';j++){
    s[i+j]=s2[j];
}
s[i+j]='\0';
puts(s);
}

strcpy

char *strcpy(char *s1,char*s2){
char *p=s1;
assert(s1!=NULL&&s2!=NULL);
while((*s1++=*s2++)!='\0');
return p;
}

strncpy

char *strncpy(char *s1,char*s2,int n){
char *p=s1;
while(n&&(*s1++=*s2++))
    n--;
if(n)
    while(--n)
    *s1++='\0';
return p;
}

strcmp函数

int strcmp(char *s1,char *s2)
{
        int t=0;
        while(!(t=*s1-*s2)&&*s2)
        {
                ++s1;
                ++s2;
        }
        return t;
}

strlen函数

int strlen(char *s1){
int t=0;
while(*s1!='\0'){
    t++;
    s1++;
}
return t;
}

等价于

int strlen(char *s1){
int t=0;
while(*s1++){
    t++;
}
return t;
}

等价于

int strlen( const char* str )
{
    const char*ptr=str;
    while (*str++ );
    return (str-ptr-1);
}

删除字符串中指定字符

int del(char *s,char c){
    int i,j;
for(i=j=0;s[i]!='\0';i++)
    if(s[i]!=c)
        s[j++]=s[i];

s[j]='\0';
}

写一函数,将两个字符串中的元音字母复制到另一个字符串,然后输出

#include<stdio.h>
int main(){
char s1[100],s2[100],c;
gets(s2);
int i,j;
for(i=j=0;(c=s2[i])!='\0';i++){
    if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'){
        s1[j++]=c;
    }
}
puts(s1);
}

字符串连接另一种方法

#include<stdio.h>
int main(){
char s1[100],s2[100],c;
gets(s1);
gets(s2);
int i,j;
for(i=0;s1[i]!='\0';i++);
for(j=0;s2[j]!='\0';j++){
    s1[i+j]=s2[j];
}
s1[i+j]='\0';
puts(s1);
}

用scanf("%s",a);输入字符串则空格之后的不再输入
用gets()可以输入一行

逆序存放

#include<stdio.h>
#include<string.h>
int main(){
    char s1[100],s2[100];
    int i,j=0;
    gets(s1);
    int n=strlen(s1)-1;
    for(i=n;i>=0;i--){
        s2[j++]=s1[i];
    }
    s2[j]='\0';
    puts(s2);
return 0;
}

删空格

int del(char *s){
    int i,j;
for(i=j=0;s[i]!='\0';i++)
    if(s[i]!=' ')
        s[j++]=s[i];

s[j]='\0';
}

保留数字字符

int del(char *s){
    int i,j;
for(i=j=0;s[i]!='\0';i++)
    if(s[i]>='0'&&s[i]<='9')
        s[j++]=s[i];

s[j]='\0';
}

保留数字字符并可以按照一定格式输出

#include<stdio.h>
int del(char *s){
    int i,j;
for(i=j=0;s[i]!='\0';i++)
    if(s[i]>='0'&&s[i]<='9')
        s[j++]=s[i];

s[j]='\0';
}
int main(){
    char s[100];
    gets(s);
    del(s);
    for(int i=0;s[i]!='\0';i++){
        printf("%3d",s[i]-48);
    }
return 0;
}

删去数字字符

int del(char *s){
    int i,j;
for(i=j=0;s[i]!='\0';i++)
    if(!(s[i]>='0'&&s[i]<='9'))
        s[j++]=s[i];

s[j]='\0';
}

输出字符串中最长的单词

#include <stdio.h>
#include <string.h>
int main()
{
	char s[100];
	gets(s);
	char max[30],temp[30];
	int i,j;
	max[0]='\0';
	for(i=0;i<strlen(s);i++){
        j=0;
        while(s[i]>='a'&&s[i]<='z'||s[i]>='A'&&s[i]<='Z')
            temp[j++]=s[i++];
    temp[j]='\0';
    if(strlen(max)<strlen(temp)){
        strcpy(max,temp);
    }
	}
	puts(max);
	printf("%d\n",strlen(max));
	return 0;
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值