quarta-feira, 28 de outubro de 2009

match.c

#include <stdio.h>


char *match(char *smaller, char *bigger)
{
 char *addr, *smaddr;
  smaddr = smaller;
  for(; *bigger != 0; bigger++){
    if(*smaller == *bigger){
     addr = bigger;
     while(*smaller == *bigger){
       if(*(smaller+1) == 0)
          return(addr);
       if(*(bigger+1) == 0)
         return(0);
      smaller++; bigger++;
     } 
     smaller = smaddr;
   }
 }
 return 0;
}

int main(int argc, char **argv)
{
  char *s1, *s2, *chk;
  s1 = "Here is what I say for the METAL:"
         " Hello"/*METAL*/" World!";
  s2 = "Hello World";
  chk = match(s2, s1);
  printf("%s", chk);
  return 0;
}

OUTPUT:
Hello World!
pf_pf_pc.c
#include <stdio.h>

char *ret_str(void){
return("Hello World");
}

char *(*(func)(void))(void){
char *(*pf)(void);
pf = ret_str;
return(pf);
}

int main(){
char *(*pff)(void);
pff = func();
printf("%s", pff());
return(0);
}

OUTPUT
Hello World
ordarray.c
#include <stdio.h>

int chkar(int *aip){
for(; *(aip+1) != 0; aip++){
if(*aip > *(aip+1))
return(1);
}
return(0);
}

void ord(int *aip){
int tmp;
for(; *(aip+1) != 0; aip++){
if(*aip > *(aip+1)){
tmp = *aip;
*aip = *(aip+1);
*(aip+1) = tmp;
}
}
}

void ordar(int *aip){
while(chkar(aip))
ord(aip);
}

int main(){
int ari[] = {2,3,7,9,4,1,5,8,6,0};
int *pi;
ordar(ari);
for(pi = ari;*pi != 0; pi++)
printf("%d, ", *pi);
putchar('\n');
return(0);
}

OUTPUT:
1, 2, 3, 4, 5, 6, 7, 8, 9,

list.c

#include <stdio.h>

struct alf
{ 
 char lt;
 struct alf *next;
}ar[27];


int main()
{
 struct alf *list_p;
 char ch, *str, *ar_p[12];
 short indx;
 ch = 97;
 str = "hello world";

 for(indx = 0; indx <= 25; indx++, ch++)
 {
  ar[indx].lt = ch;
  ar[indx].next = &ar[indx+1];
 }
 ar[26].lt = ' ';
 ar[26].next = (void *)0;


 for(indx = 0; *str != 0; str++)
 {
  list_p = ar; /* back 'a' */
  while(list_p)
  {
   if(list_p->lt == *str)
   ar_p[indx++] = &list_p->lt;
   list_p = list_p->next;
  }
 }
 *ar_p[11] = '\0'; 

 for(indx = 0; *ar_p[indx] != '\0'; indx++)
  printf("%c", *ar_p[indx]);
 putchar('\n'); 

 return(0);
}

OUTPUT:
hello world

I'm updating this becase I realy like it.. (dec 10)
struct alf is a linked list with alphabet letters, and one space
(becouse hello world have a space :P)

So.. I got an array that have pointers pointing to the right members
of the list.. I don't copy the chars... I print it from where they are
ppc.c
#include <stdio.h>

int main(){
char *p, **pp, **s;
void *malloc();
p = "hello world";
pp = (char **)malloc(sizeof(char[11]));
s = pp;
for(; *p != 0; *pp++, p++)
*pp = p;
printf("%s\n", *s);
return(0);
}


OUTPUT:
hello wolrd
mix.c

#include <stdio.h>
/*-*/

char *mix(const char *);

int main(){
char *p;
p = "hello world";
printf("%s\n", mix(p));
return(0);
}

char *mix(const char *p){
int c;
char *r, *s;
void *malloc();

r = s = (char *)malloc(sizeof p);
for(c = 0; *p != 0; c++, p++, r++){
if(c%2 == 0){
if(*p > 96 && *p < 123)
*r = *p -32;
else
*r = *p;
}else{
if(*p < 64 && *p > 91)
*r = *p +32;
else
*r = *p;
}
}
*++r = 0;
return(s);
}


OUTPUT:
HeLlO WoLrD
concat.c

#include <stdio.h>

char *concat(const char *, const char *);

int main(){
const char *p, *q;
p = "Hello ";
q = "World";
printf("%s", concat(p, q));
return(0);
}

char *concat(const char *s, const char *t){
void *malloc();
char *r, *rstart;
r = rstart = (char *)malloc(sizeof((int)s + t));
for(;*s != 0; s++, r++)
*r = *s;
}


OUTPUT:
Hello World
reverse.c

#include <stdio.h>
/* by gkos */

char *reverse(char *);

int main(){
char *string;
string = "dlrow olleh";
printf("%s\n", reverse(string));
return(0);
}

char *reverse(char *p){
char *q, *r, *s;
void *malloc();
q = p;
r = s = (char *)malloc(sizeof p);
for(;*p != 0; p++)
; /*_*/
for(--p; p != q; p--, s++)
*s = *p;
*s = *p; *++s = 0;
return(r);
}

OUTPUT:
hello world
up.c

#include <stdio.h>

char *up(const char *);

int main(){
const char *string;
string = "hello world";
printf("%s", up(string));
return(0);
}

char *up(const char *s){
char *p, *start;
void *malloc();
start = p = (char *)malloc(sizeof s);
while(*s != 0){
if(*s > 96 && *s < 123)
*p = *s - 32;
else
*p = *s;
p++; s++;
}
*++p = 0; /* end *p */
p = start;
return(p);
}




output:
HELLO WORLD
hw.c

#include <stdio.h>

int main(){
printf("Hello World");
return(0);
}

OUTPUT:
Hello World