-2

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

#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”返回“kate daniels”。如果输入是:

julie olsen

输出是:

julie olsenw

请帮忙。:(

4

5 回答 5

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
  }
}
于 2015-10-08T15:34:44.300 回答
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;
}
于 2015-10-08T18:12:08.557 回答
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
于 2015-10-08T15:39:44.950 回答
1

尝试:

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

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

    if (isalpha(string[0])){                //capitalize the first character in the string if it is a letter
        string[0] = toupper(string[0]);  //copy character to newString

    for (i=1; i <strlen(string); i++){                  
        if (string[i-1] == ' ' && isalnum(string[i]))       //if the character is preceded by a space
            string[i] = toupper(string[i]);        //copy the uppercase of the character to newString
    }
    i=strlen(string);
    while (i){
        int j= i;
        while (j && j!=' ') j--;
        printf("%.*s ", i-j,string+j);
        i=j;
    }
    return 0;
}
于 2015-10-08T15:37:10.130 回答
1

我想您还没有听说它gets()已被弃用,因此在此示例中我使用fgets(),但请注意,它保留任何尾随newline. 我的方法是将输入拆分为“令牌”,因此与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
于 2015-10-08T15:37:48.113 回答