strncpy function simulation

0

I did a function that simulates what strncpy does in the string library. Copy a number of characters from one word into another. But I do not want to change the original string ... and it does. I can not find a way to not modify it. I pass the function:

char * mistrncpy(char * cad2,
  const char * cad1, int cant) {
  const char * cadaux1;
  int i = 0;
  for (cad2 = cad1; i < cant; i++);
  if (i == cant) {
    *(cad2 + i) = '
char * mistrncpy(char * cad2,
  const char * cad1, int cant) {
  const char * cadaux1;
  int i = 0;
  for (cad2 = cad1; i < cant; i++);
  if (i == cant) {
    *(cad2 + i) = '%pre%';
    return cad2;
  }
}
'; return cad2; } }
    
asked by micaela 08.09.2017 в 05:06
source

1 answer

0

A somewhat sloppy option could be this that I present to you, sure it helps you to give you an idea.

#include <stdio.h>

void copy(const char* src,char* dest, int max){

   int index = 0;

    while(src[index]!='
#include <stdio.h>

void copy(const char* src,char* dest, int max){

   int index = 0;

    while(src[index]!='%pre%' && index<max){
        dest[index] = src[index];
        index++;
    }
    dest[index]='%pre%';//NULL
}

int main()
{
    char str1[] = "hello world";
    char str2[100];

    copy(str1,str2,9);//copia solo 9 caracteres
    printf("%s\n",str2);
    printf("%s\n",str1);

    return 0;
}
' && index<max){ dest[index] = src[index]; index++; } dest[index]='%pre%';//NULL } int main() { char str1[] = "hello world"; char str2[100]; copy(str1,str2,9);//copia solo 9 caracteres printf("%s\n",str2); printf("%s\n",str1); return 0; }
    
answered by 08.09.2017 в 05:42