strupr(char *string) 
Author Message
 strupr(char *string)

Hi All!

I've been away from the C arena for while and need a refresher.
I wish to create a function "alterme(char *)" that modifies a string
argument.

string is "1234567890"

printf("%s", string);          result -> 1234567890
printf("%s", alterme(string)); result -> 1 2 3 4 5 6 7 8 9 0
printf("%s", string);          result -> 1 2 3 4 5 6 7 8 9 0

Notice how the original string is replaced by the result of the
function.  I think this functionality is used in the STRUPR function.

proto:
        char *alterme(char *string); ???

function:
        char *alterme(char *string) {
                return *string; /* or return string  ??? */
        }

Thanks and cheerio!

--
If it's spam, it's a scam! Don't do business with net-abusers.



Fri, 26 Apr 2002 03:00:00 GMT  
 strupr(char *string)

Quote:

> Hi All!

> I've been away from the C arena for while and need a refresher.
> I wish to create a function "alterme(char *)" that modifies a string
> argument.

> string is "1234567890"

The identifier name "string" is, AFAIK, reserved for the implementation.

Quote:
> printf("%s", string);          result -> 1234567890
> printf("%s", alterme(string)); result -> 1 2 3 4 5 6 7 8 9 0
> printf("%s", string);          result -> 1 2 3 4 5 6 7 8 9 0

> Notice how the original string is replaced by the result of the
> function.  I think this functionality is used in the STRUPR function.

The C language has no strupr() or STRUPR() function.

Quote:
> proto:
>    char *alterme(char *string); ???

> function:
>    char *alterme(char *string) {
>            return *string; /* or return string  ??? */

return my_string;

Quote:
>    }

Use realloc() to enlarge the size of the parameter, then traverse the
memory are from the end, copying characters and inserting spaces. It'll
be fun.

Gergo

--
That's no moon...
                -- Obi-wan Kenobi

GU d- s:+ a--- C++>$ UL+++ P>++ L+++ E>++ W+ N++ o? K- w--- !O !M !V
PS+ PE+ Y+ PGP+ t* 5+ X- R>+ tv++ b+>+++ DI+ D+ G>++ e* h! !r !y+



Fri, 26 Apr 2002 03:00:00 GMT  
 strupr(char *string)

Quote:

>> Hi All!

>> string is "1234567890"

>The identifier name "string" is, AFAIK, reserved for the implementation.

you may be right...  the code in this message never hit a compiler. ;)

Quote:
>> Notice how the original string is replaced by the result of the
>> function.  I think this functionality is used in the STRUPR function.

>The C language has no strupr() or STRUPR() function.

Oops, yes, it's borland's function in turbo c.  But you get the point
right?

Quote:
>> proto:
>>        char *alterme(char *string); ???

>> function:
>>        char *alterme(char *string) {
>>                return *string; /* or return string  ??? */
>>        }

>return my_string;

how's that?

Quote:
>Use realloc() to enlarge the size of the parameter, then traverse the
>memory are from the end, copying characters and inserting spaces. It'll
>be fun.

actually, the intended application will make the string shorter, but
this question was geared more towards scope rather than string
manipulation functionality, thus I just arbitrarily threw out that
sample because it was easy, er, easier to explain.

Thanks though!
--
If it's spam, it's a scam! Don't do business with net-abusers.



Fri, 26 Apr 2002 03:00:00 GMT  
 strupr(char *string)
On Mon, 8 Nov 1999 12:14:56 -0800, "Dann Corbit"

Quote:

>> proto:
>> char *alterme(char *string); ???

>> function:
>> char *alterme(char *string) {
>> return *string; /* or return string  ??? */
>> }

>There is no strupr() function in the C language.  Perhaps your compiler
>offers one as an implementation extension.  

Yes, as a matter of fact it does.

Quote:
>Quite likely, all it does is
>replace each character in the original string with a capitalized version of
>it.  

Yes, and this is what I need to do, alter the original.  Thus, the
return value will be irrelevant except for purposes of testing right?
Like say, if(NULL == alterme(string)) { etc. }  Perhaps I'm thinking
incorrectly, but I thought modifying a function argument is actually
altering a copy of the original string, no?

Quote:
>Since your new strings will be twice as long, why not explore the
>malloc() function?

Sorry, poor sample.  Expanding the string is NOT gonna be implemented.

Thanks for the prompt response!

--
If it's spam, it's a scam! Don't do business with net-abusers.



Fri, 26 Apr 2002 03:00:00 GMT  
 strupr(char *string)


Quote:
> This sort of thing has a lot of landmines in it.
>     ptr = alterme("I will make your program crash");
> or:
>     char array[3] = "hi";
>     ptr = alterme(array);

> You had better think about it carefully.

I will second this. The alternatives are you have to allocate something and
deal with memory management issues outside the function (which is certainly
possible) or to have a static array stuck away somewhere where you keep the
results. The problem may well be whether you know the size well enough for
the static array to work. Not to mention other possibilities which
occasionally rear their ugly head like whether a function is re-entrant.
It's not always important, but when it is, it is a killer.

My experience with this was to generate a function that would print numbers
with commas as a visual separator. I opted for the static string and never
had any problems, but I knew for sure what the maximum possible size was for
the result.

Regards,
Sam



Fri, 26 Apr 2002 03:00:00 GMT  
 strupr(char *string)


Quote:
> On Mon, 8 Nov 1999 12:14:56 -0800, "Dann Corbit"


<snipped>

Quote:
> Yes, and this is what I need to do, alter the original.  Thus, the
> return value will be irrelevant except for purposes of testing right?
> Like say, if(NULL == alterme(string)) { etc. }  Perhaps I'm thinking
> incorrectly, but I thought modifying a function argument is actually
> altering a copy of the original string, no?

Just make sure what parameters your function receives. We had a program fail
in such a way because the parameter was "constant" and stored in 'read-only'
memory...

Modifying a function argument will work only on a copy (of an int, for
example), but modifying what a pointer points to will modify the original!
Note that modifying the pointer itself will change only the copy ...

char * alterme(char * string)
{
    *string = 'A';        /* Change the first character 'string' points to
to an 'A' */
    string = NULL;  /* Changed the actual string pointer - a copy */
    return(string);

Quote:
}

- Alain


Fri, 26 Apr 2002 03:00:00 GMT  
 strupr(char *string)

Quote:

> Hi All!

> I've been away from the C arena for while and need a refresher.
> I wish to create a function "alterme(char *)" that modifies a string
> argument.

> string is "1234567890"

> printf("%s", string);          result -> 1234567890
> printf("%s", alterme(string)); result -> 1 2 3 4 5 6 7 8 9 0
> printf("%s", string);          result -> 1 2 3 4 5 6 7 8 9 0

> Notice how the original string is replaced by the result of the
> function.  I think this functionality is used in the STRUPR function.

> proto:
>         char *alterme(char *string); ???

> function:
>         char *alterme(char *string) {
>                 return *string; /* or return string  ??? */
>         }

> Thanks and cheerio!

> --
> If it's spam, it's a scam! Don't do business with net-abusers.

This should do it...

#include <string.h>

char* alterme(char* p)
{
    unsigned i = strlen(p) ;

    if (i > 1)
    {
        unsigned j = i * 2 - 1 ;

        for (p[j] = '\0' ; i > 1 ; )
        {
            p[--j] = p[--i] ;
            p[--j] = ' ' ;
        }
    }

    return p ;

- Show quoted text -

Quote:
}



Fri, 03 May 2002 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. char * to wchar_t (char string to wide string)

2. char[] vs char * vs #defines for constant strings

3. char arrays versus char strings.

4. Conversion char/string/unsigned char?

5. how do i place an end of string char at the end of a string

6. Converting a char string into a wchar string.

7. how to use strupr() ???

8. Are strlwr() and strupr() standard?

9. strupr() not part of ANSI?

10. how do i take a string of 80 chars and strip it to a 20 chars string????

11. Converting char array of literals chars to escape chars

12. char *c,char c[ ] and char *c[ ]

 

 
Powered by phpBB® Forum Software