C函数删除所有大写字符

发布于 2024-10-16 11:35:39 字数 252 浏览 4 评论 0原文

我需要在 C 中有效实现一个函数,该函数将被赋予 char[] ,并且它将从中删除所有大写字符,返回剩下的所有内容。 例如,如果给出 HELLOmy_MANname_HOWis_AREjohn_YOU__

它应该返回 my_name_is_john__

这不是一个太容易成为的硬件,但它在我的时区是凌晨 2 点,我认为这将是一个问题的解决方案我现在面对我的代码!

欢迎任何帮助! 干杯!=)

I need an efficient implementation of a function in C that would be given a char[] and it will remove all uppercase characters from it returning everything left.
e.g. if given HELLOmy_MANname_HOWis_AREjohn_YOU__

it should return my_name_is_john__

this is not a HW too easy to be one, but its 2am in my timezone and I think this would be a solution to a problem im facing in my code now!

any help is welcome!
cheers!=)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风吹过旳痕迹 2024-10-23 11:35:39

也许这个?

i = j = 0;
while (s[i] != '\0') {
        if (!isupper(s[i]) 
                t[j++] = s[i];
        i++;
}
t[j] = '\0';

maybe this?

i = j = 0;
while (s[i] != '\0') {
        if (!isupper(s[i]) 
                t[j++] = s[i];
        i++;
}
t[j] = '\0';
╄→承喏 2024-10-23 11:35:39

算法一些伪代码怎么样?

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

How about an algorithm some pseudo-code?

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
带上头具痛哭 2024-10-23 11:35:39

这个怎么样?

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

How about this?

#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);
}
梦途 2024-10-23 11:35:39

就地工作:

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

Works in-place:

#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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文