Advertisement
Guest User

Wstawianie char* wewnątrz char*

a guest
Mar 28th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. // Solution1.cpp : Defines the entry point for the console application.
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include <conio.h>
  5. #include <string.h>
  6.  
  7. using namespace std;
  8.  
  9. int _tmain(int argc, _TCHAR* argv[])
  10. {
  11. char wyraz1[15]="01234567891234";
  12. char wyraz2[3]="ab";
  13. char* wyraz1N=new char[15];
  14. char* wyraz2N=new char[3];
  15.  
  16. strcpy(wyraz1N, wyraz1);
  17. strcpy(wyraz2N, wyraz2);
  18. cout<<"Wyraz pierwszy: "<<wyraz1N<<endl<<endl;
  19. cout<<"Wyraz do wstawienia: "<<wyraz2N<<endl<<endl;
  20.  
  21. int size1=strlen(wyraz1N);
  22. //cout<<endl<<size1;
  23.  
  24. int size2=strlen(wyraz2N);
  25. //cout<<endl<<size2;
  26.  
  27. char* bufor=new char[15];
  28. strcpy(bufor, wyraz1N);
  29.  
  30. delete [] wyraz1N;
  31.  
  32. wyraz1N=new char[size1+size2+1];
  33. size1=size1+size2+1;
  34. int n;
  35. cout<<"W jakim miejscu wstawic: ";
  36. cin>>n;
  37. cout<<endl;
  38.  
  39. for(int i=0; i<n; i++)
  40. {
  41. wyraz1N[i]=bufor[i];
  42. }
  43.  
  44. int j=0;
  45.  
  46. for(int i=n; i<n+size2+1; i++)
  47. {
  48. wyraz1N[i]=wyraz2N[j];
  49. j++;
  50. }
  51.  
  52. j=0;
  53. for(int i=n+size2; i<size1; i++)
  54. {
  55. wyraz1N[i]=bufor[n+j];
  56. j++;
  57. }
  58.  
  59. cout<<endl<<endl<<wyraz1N;
  60.  
  61. delete [] bufor;
  62. delete [] wyraz1N;
  63. delete [] wyraz2N;
  64.  
  65. _getch();
  66. return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement