Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C function to remove all uppercase characters

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!=)

like image 371
Syntax_Error Avatar asked Feb 21 '26 06:02

Syntax_Error


2 Answers

maybe this?

i = j = 0;
while (s[i] != '\0') {
        if (!isupper(s[i]) 
                t[j++] = s[i];
        i++;
}
t[j] = '\0';
like image 75
Peyman Avatar answered Feb 23 '26 19:02

Peyman


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
like image 27
Jeff Mercado Avatar answered Feb 23 '26 18:02

Jeff Mercado



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!