Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. // Este programa parece funcionar bem, mas na
  4. // realidade tem dois erros, localize-os e corrija-os...
  5. char *dupstr (char *str) {
  6.  // find the size of the original string
  7.  int size = 0;
  8.  while (str[size] != '\0')
  9.  size++;
  10.  // allocate space for the new string
  11.  char *newstr = malloc (size);
  12.  // copy original string to new string
  13.  int i;
  14.  for (i = 0; i < size; i++)
  15.  newstr[i] = str[i];
  16.  // add the terminator to the string
  17.  newstr[size] = '\0';
  18.  // return the new string
  19.  return newstr;
  20. }
  21. int main (int argc, char *arvg[]) {
  22.  char *s1 = "abcdef";
  23.  char *s2 = dupstr (s1);
  24.  printf ("Original = '%s'\n Copia = '%s'\n", s1, s2);
  25.  return 0;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement