Skip to content

Instantly share code, notes, and snippets.

@fsouza
Created June 10, 2011 04:46
Show Gist options
  • Save fsouza/1018243 to your computer and use it in GitHub Desktop.
Save fsouza/1018243 to your computer and use it in GitHub Desktop.
// caracfun.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string.h>
#include <stdlib.h>
char *replicar(char letra, int n)
{
char *str;
str = (char *)malloc((n+1)*sizeof(char));
for(int i = 0; i < n; i++)
str[i] = letra;
str[n] = '\0';
return str;
}
char *esquerda(char *s, int n)
{
char *str;
int quant_carac;
str = (char *)malloc((n+1)*sizeof(char));
if (strlen(s) < n)
quant_carac = strlen(s);
else
quant_carac = n;
for(int i = 0; i < quant_carac; i++)
str[i] = s[i];
str[quant_carac] = '\0';
return str;
}
char *direita(char *s, int n)
{
char *str;
int quant_carac;
int len_s, cont = 0;
str = (char *)malloc((n+1)*sizeof(char));
if (strlen(s) < n)
quant_carac = strlen(s);
else
quant_carac = n;
len_s = strlen(s);
for(int i = len_s - quant_carac; i < len_s ; i++)
{
str[cont] = s[i];
cont++;
}
str[quant_carac] = '\0';
return str;
}
char * numpcaraczero(int v, int c)
{
char num[100];
char *buff;
char *str;
str = (char *)malloc((c+1)*sizeof(char));
buff = replicar('0', c);
itoa(v, num, 10);
strcat(buff, num);
return direita(buff, c);
}
void insere(char *vet, char *subs, int pos)
{
char *x, *y, *s;
x = esquerda(vet, pos);
y = direita(vet, strlen(vet) - pos);
vet[0] = '\0';
strcat(vet, x);
strcat(vet, subs);
strcat(vet, y);
}
int _tmain(int argc, _TCHAR* argv[])
{
char cidade[100] = "RioJaneiro";
char numero[100] = "";
strcpy(numero, numpcaraczero(12, 10));
printf("%s\n", numero);
insere(cidade, " de ", 3);
printf("%s\n", cidade);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment