-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

(欲しいもの: narruC tranneL)

そのままでは、私が得ているのは次の出力だけです:

curran lennarta

「kate daniels」を入力すると、「kate daniels」が返されます。入力が次の場合:

julie olsen

出力は次のとおりです。

julie olsenw

助けてください。:(

4

5 に答える 5

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

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 に答える
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

「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 に答える