Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. const int INPUT_SIZE = 10;
  6. string getString();
  7. void copyValues(string, char[] );
  8. void getChars(int, int, char[], char[]);
  9. int getSubstring(char[], char[]);
  10.  
  11. int main() {
  12. char vals[INPUT_SIZE];
  13. char sub[INPUT_SIZE];
  14. string s1 = getString();
  15. if(s1.length()<=0){
  16. cout<<"Nu ati introdus nici un sir\n";
  17. return 0;
  18.  
  19. }
  20. if(s1.length()>=INPUT_SIZE){
  21. cout<<"Sirul de caractere introdus nu trebuie sa depaseasca "<<INPUT_SIZE<<" caractere\n";
  22. return 0;
  23. }
  24. copyValues(s1,vals);
  25. if(getSubstring(vals,sub)){
  26. if(strlen(sub))
  27. cout << "sub string: " << sub << endl;
  28. else cout << "Daca valorile [start] si [end] sunt egale sirul e vid\n";
  29. }
  30. return 0;
  31. }
  32. string getString() {
  33. cout << "Please type a string: ";
  34. string s;
  35. getline(cin,s);
  36. return s;
  37. }
  38. void copyValues(string s, char vals[])
  39. { int i;
  40. for (i = 0; i < s.length(); i++)
  41. vals[i] = s.at(i);
  42. vals[i] ='\0';
  43. }
  44. void getChars(int start, int end, char vals[], char newChars[]){
  45. int sz = end-start+1;
  46. // doar sz-1 caractere, sa ramana spatiu pentru terminator
  47. int i;
  48. for (i=0; i < sz - 1; i++)
  49. newChars[i] = vals[start + i];
  50. newChars[i] = '\0';
  51. }
  52. int getSubstring(char vals[],char newChars[]) {
  53. int start;
  54. int end;
  55. cout << "Starting point(integer index): ";
  56. cin >> start;
  57.  
  58. cout << "Ending point(integer index): ";
  59. cin >> end;
  60. if(start<0 || end<0 || start>strlen(vals) || end>strlen(vals)){
  61. cout<<"Valorile introduse trebuie sa fie valori numerice intregi si trebuie sa fie intre [0,"<<strlen(vals)<<")\n";
  62. return 0;
  63. }
  64. else if (start>end){
  65. cout<<"Valoarea [end] nu trebuie sa fie mai mica ca si valoarea [start]\n";
  66. return 0;
  67. }
  68. getChars(start, end, vals, newChars);
  69. return 1;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement