Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <cstdlib>
- using namespace std;
- int getRightPos(char *string, char search) {
- for (int k = strlen(string); k >= 0; k--) {
- if (string[k] == search) {
- return k;
- }
- }
- return -1;
- }
- char *getExtension(char *path) {
- int pos = getRightPos(path, '.');
- if (pos != -1) {
- char *ext = new char[strlen(path) - pos];
- for (size_t k = 0, j = pos; k <= strlen(ext); k++, j++) {
- ext[k] = path[j];
- }
- return ext;
- }
- return nullptr;
- }
- char *replaceExtension(char *oldPath, char *newExt) {
- int pos = getRightPos(oldPath, '.');
- if (pos != -1) {
- const int newSize = pos + strlen(newExt) + 1;
- char *newPath = new char[newSize];
- for (int k = 0, j = 0; k < newSize; k++) {
- if (k >= pos) {
- newPath[k] = newExt[j++];
- } else {
- newPath[k] = oldPath[k];
- }
- }
- return newPath;
- }
- return oldPath;
- }
- int main() {
- char path[210] = "D:\\Pathname\\Superpuper\\test.ltx";
- cout << getRightPos(path, '.') << endl;
- cout << getExtension(path) << endl;
- cout << replaceExtension(path, ".jpeg");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement