在大写一个字符串数组的每一个字 [英] Capitalize every word in a string array

查看:183
本文介绍了在大写一个字符串数组的每一个字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想创建一个code是大写每个单词的第一个字母一个字符串数组,然后输出以相反的顺序字符串。我无法打印反向阵,但除此之外,这是我想出了:

So I want to create a code that capitalizes the first letter of every word in a string array, then outputs the string in reverse order. I couldn't print the array in reverse, but with that aside, this is what I came up with:

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

int main() {
    char string[100];
    int i, j;
    char newString[100];

    printf("\nEnter string: ");
    gets(string);

    for (i=0; i <strlen(string); i++){                  
        if (string[i] == ' ' && isalnum(string[i+1])==1){       //if the character is preceded by a space

            newString[i] = toupper(string[i+1]);        //copy the uppercase of the character to newString
        }
        if (isalpha(string[0]) == 1){    //capitalize the first character in the string if it is a letter
            newString[0] = toupper(string[0]);          //copy character to newString
        }else{
            newString[i] = string[i];                   
        }

    }

    printf("%s", newString); //preferably, the newString should be printed in reverse order, but I can't seem to do it.
}

如果

输入:柯伦伦纳特

这code的输出咋办:柯伦伦纳特

Supposed output of this code: Curran Lennart

(我想要什么:narruC tranneL)

(What I want: narruC tranneL)

由于这是所有我得到的是一个输出:

As it is, all I'm getting is an output of:

curran lennarta

凯特·丹尼尔斯'回报'凯特·丹尼尔斯的输入。如果输入是:

An input of 'kate daniels' returns 'kate daniels'. If the input is:

julie olsen

输出是:

julie olsenw

请,帮助。 (

推荐答案

这是做(人逻辑:))最简单的方法

this is the easiest way to do that (human logics :) )

int main(void){
    int i,l,m,upper=1;
    char c;
    char buff[]="this is a simple string";

    l=strlen(buff);

    printf("Original string:\n\t'%s'\n\n",buff);

    /*capitalize*/
    for( i=0;i<l;i++){
        if(upper)
            buff[i]=toupper(buff[i]);
        upper=isspace(buff[i]);
    }
    printf("Capitalized:\n\t'%s'\n\n",buff);

    /*reversing*/
    for(i=0;i<(l/2);i++){
        c=buff[i];
        buff[i]=buff[l-(i+1)];
        buff[l-(i+1)]=c;
    }
    printf("Reversed:\n\t'%s'\n\n",buff);
    return 0;
}

这篇关于在大写一个字符串数组的每一个字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆