0

The task is as follows: to develop a program in the C++ language, using a null terminal string (a null-terminated string) to represent the text in the program, and C language tools to perform operations on this string. The program receives a sentence consisting of words separated by commas or spaces. Among the words of this sentence, finds words made up of the same letters as the first word. Move the found words to the beginning of the sentence.

I've been sitting for hours trying to figure out how pointers and functions work to perform operations on a C-style string, with no success. The last idea that came to my mind is to simply insert the words that consist of the letters of the first word of the original string at the beginning of a new string, and the rest of the words at the end of this line.

char* shiftWords(char* str) {
    char tempAnsStr[100] = "";
    char* tempStr;
    char* word1;
    char* nextStr;
    bool notnull = false;
    word1 = strtok(str, ", ");
    tempStr = strtok(NULL, ", ");
    while (tempStr) {
        if (!(check(word1, tempStr))) {
            if (notnull) {
                if (strlen(nextStr) != 0) {
                    strcat(tempAnsStr, tempStr);
                }
            }
            else {
                strcpy(tempAnsStr, tempStr);
                notnull = true;
            }
        }
        strcat(tempStr, tempAnsStr);
        strcpy(tempAnsStr, tempStr);
        tempStr = strtok(NULL, ", ");
    }
    return tempAnsStr;
}
bool check(char* A, char* B)
{
    if (strlen(A) == strlen(B)) {
        int letters_A[1024];
        char temp;
        for (int i = 0; i < strlen(A); i++) {
            letters_A[i] = int(A[i]);
        }
        for (int i = 0; i < strlen(A) - 1; i++) {
            for (int j = 0; j < strlen(A) - i - 1; j++) {
                if (letters_A[j] > letters_A[j + 1]) {
                    temp = letters_A[j];
                    letters_A[j] = letters_A[j + 1];
                    letters_A[j + 1] = temp;
                }
            }
        }
        int letters_B[1024];
        for (int i = 0; i < strlen(B); i++) {
            letters_B[i] = int(B[i]);
        }
        for (int i = 0; i < strlen(B) - 1; i++) {
            for (int j = 0; j < strlen(B) - i - 1; j++) {
                if (letters_B[j] > letters_B[j + 1]) {
                    temp = letters_B[j];
                    letters_B[j] = letters_B[j + 1];
                    letters_B[j + 1] = temp;
                }
            }
        }
        bool tf = true;
        for (int i = 0; i < strlen(A); i++) {
            if (letters_A[i] != letters_B[i]) {
                tf = false;
                break;
            }
        }
        return tf;
    }
    return false;
}
meoww
  • 11
  • I feel for you, C++ has had 30 years of development just to get rid of bugs introduced by this kind of programming. Please give this to your teacher : https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines or tell him to teach in "C" – Pepijn Kramer Apr 26 '23 at 19:09
  • What is the topic of the chapter in your C++ textbook where this practice program comes from, or the topic in your C++ class? This kind of a practice problem is typically designed for practicing one of several core C++ fundamental skills. Knowing the specific subject you're studying will be helpful in pointing you in the right direction. Are you certain that this is a C++ class or a textbook? The above code has no real C++ in it, just C. – Sam Varshavchik Apr 26 '23 at 19:09
  • 1
    The intent of your code is not 100% clear to me, so here are a few things to watch out for: Remember that `strtok` is destructive, dropping null terminators in the source string when it finds a token. A `strtok` call with a delimiter string of `", "` does not look for the string `, `, it looks for any of the characters in the string. Remember that `strlen` has a cost. If the contents of the string don't change, use `strlen` once and store the result. – user4581301 Apr 26 '23 at 19:11
  • @SamVarshavchik I disagree it is not C++ core skills, it is skills needed for writing (advanced) datastructures and or learning about how computer memory can be used at a low level (e.g. datastructures class using C++, which is different from learning C++). C++ core skills should include learning about containers/smartpointers and avoiding raw for loops... – Pepijn Kramer Apr 26 '23 at 19:11

0 Answers0