Server Help

Non-Subspace Related Coding - something useless

hellzlaker - Fri Oct 31, 2008 8:03 pm
Post subject: something useless
While doing stuff i just made 2 functions which are kind of weird i guess, i just wanted to make them and they might be useful icon_rolleyes.gif

MakeCapital() just changes a char from lower case to upper case, example "bob" will become "BOB"

Pause() will do same as system("pause"); just you can make any message like "bla bla"...


Code: Show/Hide
#include <stdio.h>
#include <windows.h>

char* MakeCapital(char* buf,int size)
{
      for(int i=0;i<size-1;++i)buf[i]-=32;
     
      return buf;
}

void Pause(const char* buf)
{
     printf("%s",buf);
     
     bool paused=true;
     
     while(paused)for(int i=0x08;i<0xFE;++i)if(GetAsyncKeyState(i))paused=false;
}

int main()
{   
     char bob[] = "bob";
     
     printf("%s",MakeCapital(bob,sizeof(bob)));
     
     Pause("\nPress a key!!!");
}

Adam01 - Fri Oct 31, 2008 8:46 pm
Post subject:
Hmmm...
For Pause(), You could of used getch(), it gets the next input, but doesnt output it.

This a string-to-caps function I think, there certainly is one for php.

But still, great ideas.
Dr Brain - Fri Oct 31, 2008 9:08 pm
Post subject:
toupper will convert characters to their uppercase equivalent without changing things like '1' or '-'.
Samapico - Sat Nov 01, 2008 1:26 am
Post subject:
As Brain said, toupper() already does that, but there are 2 things you could improve/fix in your MakeCapital function:

First, you don't need the size... A string is always ended by '\0' (a character with the value 0). So you r continuing condition can be when buf[i] != 0, or even simplier, if buf[i]. If buf[i] is non-null, evaluating "buf[i]" will return true

Code: Show/Hide
char* MakeCapital(char* buf)
{
      for(int i=0; buf[i];++i )
           buf[i]-=32;
     
      return buf;
}


Also, if you don't want characters that are already upper-case, or non-letter characters to be changed by this, you need to add a little if in there...

Code: Show/Hide
char* MakeCapital(char* buf)
{
      for(int i=0; buf[i];++i )
           if (buf[i] >= 'a' && buf[i] <= 'z')
                 buf[i]-=32;
     
      return buf;
}


And yeah, for pause, i think the function is 'getchar()'
Your function probably eats a shitload of CPU :/
Dr Brain - Sat Nov 01, 2008 8:54 am
Post subject:
toupper only converts a single char, so you'd still need a for loop like the one you've got.
Samapico - Sat Nov 01, 2008 3:26 pm
Post subject:
Dr Brain wrote:
toupper only converts a single char, so you'd still need a for loop like the one you've got.
oh, I thought there was one for strings... There's one defined in Mervbot anyway
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group