字符串处理

  • 输入一行字符串(单词和若干空格), 输出该行单词个数。
    Input:____hello_________world_ how___are___you___\n
    Output: 5
  • 输入一行字符串(单词和若干空格),输出该行单词(每个单词一行)
    Input:____hello_________world_ how___are___you___\n
    Output: hello
    world
    how
    are
    you
  • 输入一行字符串,把字符串翻转 
    Input: I am a student
    Output: student a am I
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<math.h>

int wordStat(char a[]){
    int cnt=0, i=1;
    if(a[0] != ' ')
        cnt++;
    while(a[i] != '\0'){
        if(a[i] != ' ' && a[i-1] == ' '){
            cnt++;
        }
        i++;
    }
    return cnt;
}

void wordPut(char a[]){
    int i=1;
    if(a[0] != ' ')
        printf("%c", a[0]);

    while(a[i] != '\0'){
        if(a[i] != ' '){
            printf("%c", a[i]);
        }
        else if(a[i]==' ' && a[i-1]!=' ')
            printf("\n");
        i++;
    }
    if(a[i-1] != ' ')
        printf("\n");
}

void strTurn(){
    char a[50]={0},b[50] = {0}, word[20]={0};
    int i, j, cnt, num;
    while(gets(a) != NULL){
        i = 0; j = 0; cnt = 0; num = 0;
        while(a[i] != '\0'){
            i++;
        }
        i--;
        while(i>=0){
            if(a[i] == ' '){
                printf(" ");
                b[num] = ' ';
                num++;
            }
            //word 处理
            else{
                word[j] = a[i];
                if(a[i-1]==' ' && i>0 || i==0){
                    cnt = j;
                    while(cnt>=0){
                        printf("%c", word[cnt]);
                        b[num] = word[cnt];
                        num++;
                        cnt--;
                    }
                    j = 0;
                }
                else{
                    j++;
                }
            }
            i--;
        }
        printf("\n");
        b[num] = '\0';
        printf("%s", b);
    }
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值