Top Mud Sites Forum

Top Mud Sites Forum (http://www.topmudsites.com/forums/index.php)
-   MUD Coding (http://www.topmudsites.com/forums/forumdisplay.php?f=9)
-   -   String manipulation (http://www.topmudsites.com/forums/showthread.php?t=303)

Shinji 05-25-2003 05:02 PM

I know this is going to sound like a silly notion, but i'd love to see just how it could work.

Anyone have a clue as to how a function could be written which when a string was passed to it, it would manipulate the string by capitalizing alternating letters/characters and returning the originally sent string now with alternating caps?

e.g. alt_caps("shinji is vapid") ShInJi Is VaPiD

Silly I know, but amusing and definitely of curiosity to how it might work. I'm sure if you could loop through each character in the string and apply capitalize() to every other character it would work... but how? @_@;;

KaVir 05-25-2003 06:48 PM

[code] void alt_caps( char *aStr )
{
  int i = 0;
  while ( aStr[i] = (i%2) ? (tolower(aStr[i])) ; (toupper(aStr[i])) )
     ++i;
}[/quote]

That assumes you're passing a pointer to a buffer which can be changed.  If not, you'll probably want to return a pointer to some memory allocated on the heap, or return a pointer to a static variable.

Shinji 05-25-2003 07:49 PM

My god! I think I just discovered TMI-2 doesn't support the char data type... what sense does this make!? O_O

Loriel 05-26-2003 08:22 PM

It makes perfect sense, as TMI-2 is written in LPC.

String and int datatypes between them provide all you need from the char datatype.

Ogma 05-26-2003 08:36 PM

Perhaps it would have helped if you'd stated that you wanted to do it in LPC. Kavir gave you a solution in C, a completely different language. The solution in LPC would go something like this:

[code]
string alt_caps(string str) {
int i,j;

if (!str || str=="") return 0;
str=lower_case(str);
for(i=0,j=strlen(str);i<j;i+=2)
str[i..i]=capitalize(str[i..i]);
return str;
}
[/quote]

Shinji 05-31-2003 03:44 PM

Loriel->

I still don't see why MudOS/LPC wouldn't support a basic data type found in so many other OOP related languages, I mean, assuming Lars Pensjo(sp) made that decision, why would he/or something opt to not use it?

Ogma->

[code]
string alt_caps( string str )
{
int i = strlen(str);

while( i-- )
switch( str[i] ){
case 65..90 ;
( i%2 ) ? str[i] = str[i]+32 ; str[i] = str[i];
break;
case 97..122 ;
( i%2 ) ? str[i] = str[i] ; str[i] = str[i]-32;
break;
}
return str;
}[/quote]

That's what I had come up with after KaVir put in his 2 cents, I realized he was writing in C, seeing as he developes with MUD's that are written in C it makes sense further. I apologize for not specifying it was in LPC. Despite that, his example was useful.

Loriel 05-31-2003 06:39 PM

You'd have to ask him for his reasons.

But I can't see why you want an inferior datatype when he has provided full support for the string type.

GenmaC 06-05-2003 02:20 AM

He probably didn't provide it because most scripting languages tend to avoid implementing pointers.

enigma@zebedee 06-05-2003 11:54 AM

Actually LPC does support char, just not in the way you are trying it :-p Shinji's latest example uses it - although it would be neater to use 'a' rather than the number for a (note the single quotes).

Now I sort of have an objection to this 'challenge' on principle because it makes text look like it was written by a CoOl DuDe 3lItE HaCkEr - but on the other hand it could be fun...

Hmmm...Now I like Ogma's solution - nice, clear, simple.

LPC has some fun stuff already like implode, explode, etc though which are sort of overkill for this but nice to play with.

Of course to do it as one statement there is always recursion....

convert(string str, status cap)
{
return (cap ? capitalize(str[0..0]) : lower_case(str[0..0])) + convert[str[1..], !cap);
}

Untested...so no guarantees...might do interesting things to the stack on long strings as well... ;p


All times are GMT -4. The time now is 11:00 AM.

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright Top Mud Sites.com 2022