0

I have 2 strings: 'name1' and 'name2'.

name1 is always of type something.inp and I want name 2 to be of type something.pts.

so if name1 is 'data.inp', i wanted name2 to be 'data.pts'. I tried doing the following, but with no success, since when I print name2 I get some garbage values right before 'pts'.

name1=(char*)malloc((strlen(argv[2])+1)*sizeof(char));

name2=(char*)malloc(1+(strlen(name1))*sizeof(char));

strcpy(name1, argv[2]);



strncpy(name2, name1, strlen(name1)-3);


strcat(name2, "pts");

Help will be greatly appreciated.

Rui Loureiro
  • 119
  • 9

1 Answers1

0

You can do it remplacing the last characters :

name1 = argv[2];
name2 = name1;
name2[name2.length()-3] = 'p';
name2[name2.length()-2] = 't';
name2[name2.length()-1] = 's';
BNilsou
  • 886
  • 8
  • 22