Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <string.h>
- using namespace std;
- string inputString();
- int inputForceInteger(string);
- char* copyStrs(string, char *);
- char* subStrs(char*,char *);
- char* getStrs(int,int,char *,char *);
- int main()
- {
- char *strs;
- char *sub;
- string s1 = inputString();
- strs = copyStrs(s1, strs);
- sub = subStrs(strs,sub);
- std::cout << "sub string: [" << sub << "]" << endl;
- return 0;
- }
- string inputString()
- {
- cout << "Type a string: ";
- string s;
- getline(cin,s, '\n');
- return s;
- }
- char* copyStrs(string s, char *strs)
- {
- strs = (char*) malloc(s.length() + 1 * sizeof(char));
- int i = 0;
- for (i = 0; i < s.length(); i++) strs[i] = s.at(i);
- strs[i] ='\0';
- return strs;
- }
- char* subStrs(char* strs, char* newStrs)
- {
- int start;
- int end;
- // 2 - error when input # integer type
- start = inputForceInteger("Start point (Integer value): ");
- end = inputForceInteger("End point (Integer value): ");
- newStrs = getStrs(start,end,strs,newStrs);
- return newStrs;
- }
- int inputForceInteger(string cmt)
- {
- string s;
- do
- {
- cout << cmt;
- getline(cin,s, '\n');
- } while(atol(s.c_str()) == 0 && !(s.length() == 1 && s[0] == '0'));
- return atoi(s.c_str());
- }
- char* getStrs(int start,int end,char* strs,char* newStrs)
- {
- if(end > strlen(strs))
- {
- cout << "End value too large" << endl;
- return "\0";
- }
- int len = end - start + 1;
- newStrs = (char*) calloc (len, sizeof(char));
- int i = 0;
- for (i=0; i < len - 1; i++) newStrs[i] = strs[start + i];
- newStrs[i] = '\0';
- return newStrs;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement