// Program to perform various string operations. // Assignment-08 // stroper.cpp, Author - Krishna Kumar Khatri. // Uncomment ONLY the following line if you are using Turbo/Borland compiler. // #define TC #ifdef TC #include #else #include using namespace std; #endif #include // For getch. const int MAXLEN = 31; int len(const char str[]) { int ln; for (ln = 0; str[ln] != '\0'; ln++) ; return ln; } char* copy(char strd[], const char strs[]) { int ln = len(strs); int i; for (i = 0; i < ln; i++) strd[i] = strs[i]; strd[i] = '\0'; return strd; } char* concat(const char strf[], const char strs[]) { int ln1 = len(strf); int ln2 = len(strs); char* res = new char[2 * MAXLEN]; int i, j, flag = 0; for (i = 0, j = 0; i < ln1 + ln2; i++, j++) { if (strf[j] == '\0') { flag = 1; j = 0; } flag == 0 ? res[i] = strf[j] : res[i] = strs[j]; } res[i] = '\0'; return res; } char* ucase(const char str[]) { char* res = new char[MAXLEN]; int ln = len(str); int i; for (i = 0; i < ln; i++) str[i] >= 97 && str[i] <= 122 ? res[i] = str[i] - 32 : res[i] = str[i]; res[i] = '\0'; return res; } char* lcase(const char str[]) { char* res = new char[MAXLEN]; int ln = len(str); int i; for (i = 0; i < ln; i++) str[i] >= 65 && str[i] <= 90 ? res[i] = str[i] + 32 : res[i] = str[i]; res[i] = '\0'; return res; } short compare(const char str1[], const char str2[]) { int ln = len(str1) < len(str2) ? len(str1) : len(str2); int i; for (i = 0; i < ln; i++) if (str1[i] != str2[i]) return str1[i] < str2[i++] ? -1 : 1; return 0; } void breakup(const char str[], int pos) { int i, j; char* str1 = new char[pos + 1]; char* str2 = new char[len(str) - pos]; for (i = 0, j = 0; i < pos; i++) str1[j++] = str[i]; str1[j] = '\0'; for (j = 0; i < len(str); i++) str2[j++] = str[i]; str2[j] = '\0'; cout << str1 << " and " << str2; } int main() { #ifdef TC clrscr(); #endif char* str1 = new char[MAXLEN]; char* str2 = new char[MAXLEN]; char* str3 = new char[2 * MAXLEN]; int brpos; cout << "String termination character is tilde(~)." << endl << endl; cout << "Enter the first string (max. " << MAXLEN - 1<< "): "; cin.getline(str1, MAXLEN, '~'); // Flush the buffer to avoid newline char appended to str2. cout << flush; // Emptying buffer. cout << "Enter the second string (max. " << MAXLEN - 1<< "): "; cin.getline(str2, MAXLEN, '~'); cout << endl << "Strlen(" << str1 << ") = " << len(str1); cout << endl << "Copied string is, " << copy(str3, str1); cout << endl << "Concatenation yielded, " << concat(str1, str2); cout << endl << "UpperCase(" << str1 << ") = " << ucase(str1); cout << endl << "LowerCase(" << str1 << ") = " << lcase(str1); cout << endl << "Comparison result, "; switch (compare(str1, str2)) { case -1: cout << str1 << " < " << str2; break; case 0: cout << str1 << " = " << str2; break; case 1: cout << str1 << " > " << str2; break; } do { cout << endl << "Enter first string's desired length: "; cin >> brpos; } while (brpos < 1 || brpos > len(str1)); cout << "Two strings now constructed are, "; breakup(str1, brpos); cout << endl << endl << "Program over. Press any key to exit..."; getch(); return 0; // Successful termination. }