User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion

Reply
Thread Tools Display Modes
Unread 16 Oct 2003, 22:57   #1
MT
/dev/zero
Retired Mod
 
MT's Avatar
 
Join Date: May 2000
Posts: 415
MT is an unknown quantity at this point
Flavius

Code:
#include <stdio.h>
#include <string.h>

int strmtcmp(char*,char*);

int main() {
  char a[] = "hello world";
  char b[] = "hello wo";
  int res = strmtcmp(a,b);
  if (res>0)
    printf("'%s' is 'larger' than '%s'\n",a,b);
  else if (res<0)
    printf("'%s' is 'smaller' than '%s'\n",a,b);
  else
    printf("'%s' is the same as '%s'\n",a,b);
  /* prove it is correct */
  printf("%d - %d\n",res,strcmp(a,b));
  return 0;
}

int strmtcmp(char* a,char* b) {
  while(    *a != '\0' /* not at end of string 1 */
        &&  *b != '\0' /* not at end of string 2 */
        &&  *a == *b ) /* they are the same at the same char */
  {
    a++; b++; /* move to next element */
  }
  /* finished the loop - either a is null, b is null, or the chars differ */
  return (*a - *b);
}
Code:
[email protected] [11:01 PM]  '~'
> $ gcc -Wall -o flavius flavius.c && ./flavius
'hello world' is 'larger' than 'hello wo'
114 - 114
__________________
#linux : Home of Genius

<idimmu> ok i was chained to a desk with this oriental dude
MT is offline   Reply With Quote
Unread 16 Oct 2003, 23:08   #2
Flavius
 
Join Date: Jan 2002
Posts: 421
Flavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet society
Re: Flavius

thank u

would have never been able to figure it out myself

is it ok to use already-made algorithms ?
Flavius is offline   Reply With Quote
Unread 16 Oct 2003, 23:43   #3
Flavius
 
Join Date: Jan 2002
Posts: 421
Flavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet society
Re: Flavius

Code:
int f_p_strcmp(const char *a, const char *b) {
	while( *a! = '\0'	// not at end of string 1
		&& *b! = '\0'	// not at end of string 2
		&& *a == *b ) { // they are the same at the same char
		a++; b++;	// go over to next element
	}
	// finished the loop - either a is null, b is null, or the chars differ
	return (*a-*b);
}
char *f_p_strcpy(char *dst,const char *src) {
	while (*src!='\0') { *dst++ = *src++; }	// copy source to destination and move pointers
	*dst = '\0';				// dont forget to finish destination
	return dst;
}
Flavius is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Flavius Ragnarak General Discussions 7 3 Jul 2003 00:34
Love and etc (not the same as Flavius) Mong General Discussions 78 2 Jul 2003 10:42
The perfect girl... Chojin General Discussions 17 23 Jun 2003 01:24
Flavius Aryn General Discussions 2 10 Jan 2003 18:02


All times are GMT +1. The time now is 10:08.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018