Top Mud Sites Forum Return to TopMudSites.com
Go Back   Top Mud Sites Forum > Mud Development and Administration > MUD Coding
Click here to Register

Reply
 
Thread Tools
Old 05-25-2003, 05:02 PM   #1
Shinji
New Member
 
Join Date: Jul 2002
Location: Okinawa, Japan
Posts: 7
Shinji is on a distinguished road
Send a message via AIM to Shinji
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? @_@;;
Shinji is offline   Reply With Quote
Old 05-25-2003, 06:48 PM   #2
KaVir
Legend
 
KaVir's Avatar
 
Join Date: Apr 2002
Name: Richard
Home MUD: God Wars II
Posts: 2,052
KaVir will become famous soon enoughKaVir will become famous soon enough
[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.
KaVir is offline   Reply With Quote
Old 05-25-2003, 07:49 PM   #3
Shinji
New Member
 
Join Date: Jul 2002
Location: Okinawa, Japan
Posts: 7
Shinji is on a distinguished road
Send a message via AIM to Shinji
My god! I think I just discovered TMI-2 doesn't support the char data type... what sense does this make!? O_O
Shinji is offline   Reply With Quote
Old 05-26-2003, 08:22 PM   #4
Loriel
Member
 
Join Date: May 2002
Posts: 49
Loriel is on a distinguished road
Quote:
Originally Posted by
My god! I think I just discovered TMI-2 doesn't support the char data type... what sense does this make!?
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.
Loriel is offline   Reply With Quote
Old 05-26-2003, 08:36 PM   #5
Ogma
Member
 
Ogma's Avatar
 
Join Date: Apr 2002
Home MUD: DartMUD
Posts: 86
Ogma is on a distinguished road
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]
Ogma is offline   Reply With Quote
Old 05-31-2003, 03:44 PM   #6
Shinji
New Member
 
Join Date: Jul 2002
Location: Okinawa, Japan
Posts: 7
Shinji is on a distinguished road
Send a message via AIM to Shinji
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.
Shinji is offline   Reply With Quote
Old 05-31-2003, 06:39 PM   #7
Loriel
Member
 
Join Date: May 2002
Posts: 49
Loriel is on a distinguished road
Quote:
Originally Posted by
Shinji wrote : 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?
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.
Loriel is offline   Reply With Quote
Old 06-05-2003, 02:20 AM   #8
GenmaC
Member
 
Join Date: May 2002
Posts: 68
GenmaC is on a distinguished road
He probably didn't provide it because most scripting languages tend to avoid implementing pointers.
GenmaC is offline   Reply With Quote
Old 06-05-2003, 11:54 AM   #9
enigma@zebedee
Member
 
Join Date: Mar 2003
Posts: 70
enigma@zebedee is on a distinguished road
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
enigma@zebedee is offline   Reply With Quote
Reply


Thread Tools


String manipulation - Similar Threads
Thread Thread Starter Forum Replies Last Post
Coding Contest: Mud String Library Kyndig MUD Announcements 2 06-23-2006 03:05 PM

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off

All times are GMT -4. The time now is 03:52 PM.


Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Style based on a design by Essilor
Copyright Top Mud Sites.com 2014