Código:
#include<stdio.h> int factorial(int n) { int r=1,i; for( i = 1; i <= n; i++) r *= i; return r; } int main() { int r,x = 8; r = factorial( x ); printf("factorial ( %d ) = %d \n",x,r); return 0; }
Salida:
factorial ( 8 ) = 40320
#include<stdio.h> int factorial(int n) { int r=1,i; for( i = 1; i <= n; i++) r *= i; return r; } int main() { int r,x = 8; r = factorial( x ); printf("factorial ( %d ) = %d \n",x,r); return 0; }
factorial ( 8 ) = 40320
string
#include<string.h> void *memchr(const void *s,int c,size_t n);
#include<stdio.h> #include<string.h> int main(){ char s[]="programas en c"; char *t=memchr(s,'e',strlen(s)); printf("es --> %s\n",t); return 0; }
es --> en c
string.h
char *strcpy ( char *destino , const char *fuente );
destino Cadena que almacenará una copia de 'fuente'. fuente Cadena que se copiará.
Se retorna la cadena 'destino'.
#include<stdio.h> #include<string.h> int main(){ char fuente[100]="hola mundo"; char destino1[100],destino2[100]; char *retorno1,*retorno2; retorno1=strcpy(destino1,fuente); retorno2=strcpy(destino2,"cadena a copiar"); printf("fuente: %s\n",fuente); printf("destino1: %s\n",destino1); printf("retorno1: %s\n",retorno1); printf("destino2: %s\n",destino2); printf("retorno2: %s\n",retorno2); return 0; }
fuente: hola mundo destino1: hola mundo retorno1: hola mundo destino2: cadena a copiar retorno2: cadena a copiar
#include<stdio.h> char *mi_strcpy(char *destino,char *fuente){ int i; for(i=0;fuente[i]!='\0';i++)destino[i]=fuente[i]; destino[i]='\0'; return destino; } int main(){ char fuente[100]="hola mundo"; char destino1[100],destino2[100]; char *retorno1,*retorno2; retorno1=mi_strcpy(destino1,fuente); retorno2=mi_strcpy(destino2,"cadena a copiar"); printf("fuente: %s\n",fuente); printf("destino1: %s\n",destino1); printf("retorno1: %s\n",retorno1); printf("destino2: %s\n",destino2); printf("retorno2: %s\n",retorno2); return 0; }
fuente: hola mundo destino1: hola mundo retorno1: hola mundo destino2: cadena a copiar retorno2: cadena a copiar
string.h
size_t strlen ( const char *string );
string La cadena a procesar.
Longitud de la cadena.
#include<stdio.h> #include<string.h> int main(){ char string[100]; printf("Ingrese el string: "); scanf("%s",string); int tam=strlen(string); printf("Su longitud es : %d\n",tam); return 0; }
Ingrese el string: algoritmos Su longitud es : 10
#include<stdio.h> int mi_strlen(char *string){ int i; for(i=0;string[i]!='\0';i++); return i; } int main(){ char string[100]; printf("Ingrese el string: "); scanf("%s",string); int tam=mi_strlen(string); printf("Su longitud es : %d\n",tam); return 0; }
Ingrese el string: algoritmos Su longitud es : 10