Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- const int INPUT_SIZE = 10;
- string getString();
- void copyValues(string, char[] );
- void getChars(int, int, char[], char[]);
- int getSubstring(char[], char[]);
- int main() {
- char vals[INPUT_SIZE];
- char sub[INPUT_SIZE];
- string s1 = getString();
- if(s1.length()<=0){
- cout<<"Nu ati introdus nici un sir\n";
- return 0;
- }
- if(s1.length()>=INPUT_SIZE){
- cout<<"Sirul de caractere introdus nu trebuie sa depaseasca "<<INPUT_SIZE<<" caractere\n";
- return 0;
- }
- copyValues(s1,vals);
- if(getSubstring(vals,sub)){
- if(strlen(sub))
- cout << "sub string: " << sub << endl;
- else cout << "Daca valorile [start] si [end] sunt egale sirul e vid\n";
- }
- return 0;
- }
- string getString() {
- cout << "Please type a string: ";
- string s;
- getline(cin,s);
- return s;
- }
- void copyValues(string s, char vals[])
- { int i;
- for (i = 0; i < s.length(); i++)
- vals[i] = s.at(i);
- vals[i] ='\0';
- }
- void getChars(int start, int end, char vals[], char newChars[]){
- int sz = end-start+1;
- // doar sz-1 caractere, sa ramana spatiu pentru terminator
- int i;
- for (i=0; i < sz - 1; i++)
- newChars[i] = vals[start + i];
- newChars[i] = '\0';
- }
- int getSubstring(char vals[],char newChars[]) {
- int start;
- int end;
- cout << "Starting point(integer index): ";
- cin >> start;
- cout << "Ending point(integer index): ";
- cin >> end;
- if(start<0 || end<0 || start>strlen(vals) || end>strlen(vals)){
- cout<<"Valorile introduse trebuie sa fie valori numerice intregi si trebuie sa fie intre [0,"<<strlen(vals)<<")\n";
- return 0;
- }
- else if (start>end){
- cout<<"Valoarea [end] nu trebuie sa fie mai mica ca si valoarea [start]\n";
- return 0;
- }
- getChars(start, end, vals, newChars);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement