2

char[] が与えられる C の関数を効率的に実装する必要があり、すべての大文字を削除して、残っているすべてを返します。例えば与えられた場合HELLOmy_MANname_HOWis_AREjohn_YOU__

それは戻るべきですmy_name_is_john__

これは簡単すぎるハードウェアではありませんが、私のタイムゾーンでは午前 2 時であり、これは私のコードで現在直面している問題の解決策になると思います!

どんな助けでも大歓迎です!乾杯!=)

4

4 に答える 4

10

たぶんこれ?

i = j = 0;
while (s[i] != '\0') {
        if (!isupper(s[i]) 
                t[j++] = s[i];
        i++;
}
t[j] = '\0';
于 2011-02-07T23:43:09.377 に答える
4

いくつかの擬似コードのアルゴリズムはどうですか?

initialize a rewrite pointer to the beginning of the string
for each character in the input string that isn't nul:
    if character is not an uppercase letter:
        add the character to rewrite pointer
        increment rewrite pointer
add nul terminator to rewrite pointer
于 2011-02-07T23:39:17.317 に答える
2

これはどう?

#include <string.h> //strlen, strcpy
#include <ctype.h>  //isupper
#include <stdlib.h> //calloc, free

//removes uppercase characters
void rem_uc(char *str) {
    char *newStr = calloc(strlen(str), sizeof(char));
    char curChar;
    int i_str = 0, i_newStr = 0;
    do {
        curChar = str[i_str];
        if(!isupper(curChar)) {
            newStr[i_newStr] = curChar;
            i_newStr++;
        }
        i_str++;
    } while(curChar != 0);
    strcpy(str, newStr);
    free(newStr);
}
于 2011-02-08T00:02:32.320 に答える
1

インプレースで動作:

#include <stdio.h>
#include <stdlib.h>

char* removeUpperCase(char *s) {
    char *current = s;
    char *r = s; // r is the same rewrite pointer someone else mentioned in his answer =)
    do {
        if ((*current < 'A') || (*current > 'Z')) {
            *r++ = *current;
        }
    } while (*current++ != 0);
    return s;
}

int main() {
    char *s = strdup("HELLOmy_MANname_HOWis_AREjohn_YOU__"); // needed because constants cannot be modified
    printf(removeUpperCase(s));
    free(s);
    return 0;
}
于 2011-02-08T00:38:07.000 に答える