【问题标题】:Capitalize every word in a string array将字符串数组中的每个单词大写
【发布时间】:2015-10-08 15:17:48
【问题描述】:

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

#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

请帮忙。 :(

【问题讨论】:

  • man isalpha 给出RETURN VALUE : The values returned are nonzero if the character c falls into the tested class, and zero if not.。因此,即使 isalnum(string[i+1])==1 是字母数字字符,您也无法确定 true

标签: c arrays string toupper


【解决方案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
  }
}

【讨论】:

    【解决方案2】:

    试试:

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

    【讨论】:

      【解决方案3】:

      '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】:

        我想你还没有听说过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
        

        【讨论】:

          【解决方案5】:

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

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

          【讨论】:

            猜你喜欢
            • 2010-10-16
            • 2011-05-15
            • 1970-01-01
            • 2014-06-19
            • 1970-01-01
            • 2011-01-20
            • 1970-01-01
            • 2020-12-22
            • 2010-12-25
            相关资源
            最近更新 更多