大写字符串数组中的每个单词

时间:2015-10-08 15:17:48

标签: c arrays string toupper

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

#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.
}

如果:

输入:curran lennart

假设此代码的输出:Curran Lennart

(我想要的:narruC tranneL)

实际上,我得到的只是输出:

curran lennarta

kate daniels&#39;的输入。返回凯特丹尼尔斯&#39;。如果输入是:

julie olsen

输出结果为:

julie olsenw

请帮忙。 :(

5 个答案:

答案 0 :(得分:1)

您可以使用strtok将字符串拆分为指定的分隔符(在本例中为空格),然后当您将单词拆分为大写第一个字符时。

char input[] = "A bird came down the walk";
    printf("Parsing the input string '%s'\n", input);
    char *token = strtok(input, " ");
    while(token) {
        puts(token);
        token = strtok(NULL, " ");
    }

    printf("Contents of the input string now: '");
    for(size_t n = 0; n < sizeof input; ++n)
        input[n] ? printf("%c", input[n]) : printf("\\0");
    puts("'");

然后你翻转字符串中的所有字母

void inplace_reverse(char * str)
{
  if (str)
  {
    char * end = str + strlen(str) - 1;

    // swap the values in the two given variables
    // XXX: fails when a and b refer to same memory location
#   define XOR_SWAP(a,b) do\
    {\
      a ^= b;\
      b ^= a;\
      a ^= b;\
    } while (0)

    // walk inwards from both ends of the string, 
    // swapping until we get to the middle
    while (str < end)
    {
      XOR_SWAP(*str, *end);
      str++;
      end--;
    }
#   undef XOR_SWAP
  }
}

答案 1 :(得分:1)

尝试:

compileSdkVersion

答案 2 :(得分:1)

我想您还没有听说gets()已被弃用,因此在此示例中我使用fgets(),但要注意,它会保留任何尾随newline。我的方法是将输入分成&#34;标记&#34;,因此与空格同时处理newline

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

int main(void) {
    char string[100];
    char *tptr;
    size_t i, len;

    printf("\nEnter string: ");
    if (fgets(string, sizeof(string), stdin) == NULL)
        return 1;

    tptr = strtok(string, " \n\r\t");
    while (tptr != NULL) {
        tptr[0] = toupper(tptr[0]);
        len = strlen(tptr);
        for(i=0; i<len; i++)
            printf("%c", tptr[len-1-i]);
        tptr = strtok(NULL, " \n\r\t");
        if (tptr != NULL)
            printf(" ");
    }
    printf("\n");
    return 0;
}

使用您的数据运行示例

Enter string: curran lennart
narruC tranneL

Enter string: kate daniels
etaK sleinaD

Enter string: julie olsen
eiluJ neslO

答案 3 :(得分:1)

'A'的ascii值为65,'a'为97

所以这就是算法

->accept the string ,now read the array character by character till null character
-> if(character>=97 and character<=122)
       character=character-32
->now reverse the string using standard library function strrev(string)
->print it

答案 4 :(得分:1)

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

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;
}