Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C program(homework)

C program(homework)

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresregex
12 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • OriginalGriffO OriginalGriff

    While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you. So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in! Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did. Just doing a copy'n'paste of your assignment will get you nowhere. If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

    M Offline
    M Offline
    Member_16311074
    wrote on last edited by
    #3

    thanks,I got to the point of searching input and limiting it to 10 characters but I dont know how to replace that searched words.

    OriginalGriffO 1 Reply Last reply
    0
    • M Member_16311074

      thanks,I got to the point of searching input and limiting it to 10 characters but I dont know how to replace that searched words.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #4

      And? What have you tried? Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      M 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        And? What have you tried? Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

        M Offline
        M Offline
        Member_16311074
        wrote on last edited by
        #5

        #include
        #include

        #define MAX_STRINGS 3
        #define MAX_LENGTH 512

        // Function to perform search and replace
        void searchAndReplace(char text[][MAX_LENGTH], int numStrings, const char* searchStr, const char* replaceStr) {
        int replaced = 0;

        for (int i = 0; i < numStrings; ++i) {
            char buffer\[MAX\_LENGTH\];
            char\* pos;
            size\_t searchLen = strlen(searchStr);
            size\_t replaceLen = strlen(replaceStr);
        
            // Copy original string to buffer
            strcpy\_s(buffer, text\[i\]);
        
            // Perform the replacement
            while ((pos = strstr(buffer, searchStr)) != NULL) {
                // Print the original string before replacement
                if (!replaced) {
                    printf("%s\\n", text\[i\]);
                    replaced = 1;
                }
        
                // Perform the replacement
                size\_t len = strlen(pos + searchLen);
                memmove(pos + replaceLen, pos + searchLen, len + 1);
                memcpy(pos, replaceStr, replaceLen);
            }
        
            // Print the modified string if it was replaced
            if (replaced) {
                printf("%s\\n", buffer);
                replaced = 0; // Reset for the next string
            }
        }
        

        }

        int main() {
        // Prepare the strings in full-width characters
        char strings[MAX_STRINGS][MAX_LENGTH] = {
        "18歳と81歳の違い。18歳は恋に溺れる人で、81歳はお風呂に溺れる人。18歳は道を走り、逆もまた然り。81歳の人が走っている。18歳は心がもろく、81歳は骨がもろい。私は18歳で心臓が止まらない。",
        "81歳の彼は飛行機を止められない。18歳は恋に窒息することができ、81歳は餅に窒息することができる。偏差値が気になる。私は18歳で、81歳で血圧と血糖値を気にしている。まだ何も知らない18歳、もう何も覚えていない81歳。",
        "自分を探している18歳と、みんなを探している81歳。"
        };

        // Display the original strings
        printf("Original strings:\\n");
        for (int i = 0; i < MAX\_STRINGS; ++i) {
            printf("%s\\n", strings\[i\]);
        }
        
        // Input search and replacement strings
        char searchStr\[11\], replaceStr\[11\];
        printf("Enter search string (up to 10 characters): ");
        fgets(searchStr, sizeof(searchStr), stdin);
        searchStr\[strcspn(searchStr, "\\n")\] = '\\0'; // Remove newline character
        printf("Enter replacement string (up to 10 characters): ");
        fgets(replaceStr, sizeof(replaceStr), stdin);
        replaceStr\[strcspn(replaceStr, "\\n")\] = '\\0'; // Remove newline character
        
        // Ensure searchStr and replaceStr are within the allowed length
        if (strlen(searchStr) > 10 || strlen(replaceStr) > 10) {
            fprintf(stderr, "Search and replacement strings must be up to 10 characters long.\\n");
            return 1;
        }
        
        // Process
        
        OriginalGriffO D 2 Replies Last reply
        0
        • M Member_16311074

          #include
          #include

          #define MAX_STRINGS 3
          #define MAX_LENGTH 512

          // Function to perform search and replace
          void searchAndReplace(char text[][MAX_LENGTH], int numStrings, const char* searchStr, const char* replaceStr) {
          int replaced = 0;

          for (int i = 0; i < numStrings; ++i) {
              char buffer\[MAX\_LENGTH\];
              char\* pos;
              size\_t searchLen = strlen(searchStr);
              size\_t replaceLen = strlen(replaceStr);
          
              // Copy original string to buffer
              strcpy\_s(buffer, text\[i\]);
          
              // Perform the replacement
              while ((pos = strstr(buffer, searchStr)) != NULL) {
                  // Print the original string before replacement
                  if (!replaced) {
                      printf("%s\\n", text\[i\]);
                      replaced = 1;
                  }
          
                  // Perform the replacement
                  size\_t len = strlen(pos + searchLen);
                  memmove(pos + replaceLen, pos + searchLen, len + 1);
                  memcpy(pos, replaceStr, replaceLen);
              }
          
              // Print the modified string if it was replaced
              if (replaced) {
                  printf("%s\\n", buffer);
                  replaced = 0; // Reset for the next string
              }
          }
          

          }

          int main() {
          // Prepare the strings in full-width characters
          char strings[MAX_STRINGS][MAX_LENGTH] = {
          "18歳と81歳の違い。18歳は恋に溺れる人で、81歳はお風呂に溺れる人。18歳は道を走り、逆もまた然り。81歳の人が走っている。18歳は心がもろく、81歳は骨がもろい。私は18歳で心臓が止まらない。",
          "81歳の彼は飛行機を止められない。18歳は恋に窒息することができ、81歳は餅に窒息することができる。偏差値が気になる。私は18歳で、81歳で血圧と血糖値を気にしている。まだ何も知らない18歳、もう何も覚えていない81歳。",
          "自分を探している18歳と、みんなを探している81歳。"
          };

          // Display the original strings
          printf("Original strings:\\n");
          for (int i = 0; i < MAX\_STRINGS; ++i) {
              printf("%s\\n", strings\[i\]);
          }
          
          // Input search and replacement strings
          char searchStr\[11\], replaceStr\[11\];
          printf("Enter search string (up to 10 characters): ");
          fgets(searchStr, sizeof(searchStr), stdin);
          searchStr\[strcspn(searchStr, "\\n")\] = '\\0'; // Remove newline character
          printf("Enter replacement string (up to 10 characters): ");
          fgets(replaceStr, sizeof(replaceStr), stdin);
          replaceStr\[strcspn(replaceStr, "\\n")\] = '\\0'; // Remove newline character
          
          // Ensure searchStr and replaceStr are within the allowed length
          if (strlen(searchStr) > 10 || strlen(replaceStr) > 10) {
              fprintf(stderr, "Search and replacement strings must be up to 10 characters long.\\n");
              return 1;
          }
          
          // Process
          
          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #6

          Why not? What did the debugger show you was happening?

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          M 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            Why not? What did the debugger show you was happening?

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

            M Offline
            M Offline
            Member_16311074
            wrote on last edited by
            #7

            the debugger let me input my search string and not letting me enter my replacement string. I made it so that 10 letters can be enter into it but it stop working when I put letters over 3. Are there any chance the program is mistaking one japanese word into 3 letters?

            OriginalGriffO D R 4 Replies Last reply
            0
            • M Member_16311074

              the debugger let me input my search string and not letting me enter my replacement string. I made it so that 10 letters can be enter into it but it stop working when I put letters over 3. Are there any chance the program is mistaking one japanese word into 3 letters?

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #8

              Assuming that your Japanese is in Unicode (Hiragana / Katakana) then it'll usually be 2 bytes per character, but it is possible to use three byte Unicode (since the possible range is 21 bits, meaning 3 bytes) What exactly do you mean "not letting me enter my replacement string." I'm pretty sure it didn't try to smack your wrist and tell you off but what exactly did happen? How did you check?

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              • M Member_16311074

                the debugger let me input my search string and not letting me enter my replacement string. I made it so that 10 letters can be enter into it but it stop working when I put letters over 3. Are there any chance the program is mistaking one japanese word into 3 letters?

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #9

                char on it's own is 1 byte - for Unicode you probably need to use the uchar.h header and its' types, but I've never needed them so I can't help you that much. You might want to check your course notes and Google before going any further.

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                1 Reply Last reply
                0
                • M Member_16311074

                  #include
                  #include

                  #define MAX_STRINGS 3
                  #define MAX_LENGTH 512

                  // Function to perform search and replace
                  void searchAndReplace(char text[][MAX_LENGTH], int numStrings, const char* searchStr, const char* replaceStr) {
                  int replaced = 0;

                  for (int i = 0; i < numStrings; ++i) {
                      char buffer\[MAX\_LENGTH\];
                      char\* pos;
                      size\_t searchLen = strlen(searchStr);
                      size\_t replaceLen = strlen(replaceStr);
                  
                      // Copy original string to buffer
                      strcpy\_s(buffer, text\[i\]);
                  
                      // Perform the replacement
                      while ((pos = strstr(buffer, searchStr)) != NULL) {
                          // Print the original string before replacement
                          if (!replaced) {
                              printf("%s\\n", text\[i\]);
                              replaced = 1;
                          }
                  
                          // Perform the replacement
                          size\_t len = strlen(pos + searchLen);
                          memmove(pos + replaceLen, pos + searchLen, len + 1);
                          memcpy(pos, replaceStr, replaceLen);
                      }
                  
                      // Print the modified string if it was replaced
                      if (replaced) {
                          printf("%s\\n", buffer);
                          replaced = 0; // Reset for the next string
                      }
                  }
                  

                  }

                  int main() {
                  // Prepare the strings in full-width characters
                  char strings[MAX_STRINGS][MAX_LENGTH] = {
                  "18歳と81歳の違い。18歳は恋に溺れる人で、81歳はお風呂に溺れる人。18歳は道を走り、逆もまた然り。81歳の人が走っている。18歳は心がもろく、81歳は骨がもろい。私は18歳で心臓が止まらない。",
                  "81歳の彼は飛行機を止められない。18歳は恋に窒息することができ、81歳は餅に窒息することができる。偏差値が気になる。私は18歳で、81歳で血圧と血糖値を気にしている。まだ何も知らない18歳、もう何も覚えていない81歳。",
                  "自分を探している18歳と、みんなを探している81歳。"
                  };

                  // Display the original strings
                  printf("Original strings:\\n");
                  for (int i = 0; i < MAX\_STRINGS; ++i) {
                      printf("%s\\n", strings\[i\]);
                  }
                  
                  // Input search and replacement strings
                  char searchStr\[11\], replaceStr\[11\];
                  printf("Enter search string (up to 10 characters): ");
                  fgets(searchStr, sizeof(searchStr), stdin);
                  searchStr\[strcspn(searchStr, "\\n")\] = '\\0'; // Remove newline character
                  printf("Enter replacement string (up to 10 characters): ");
                  fgets(replaceStr, sizeof(replaceStr), stdin);
                  replaceStr\[strcspn(replaceStr, "\\n")\] = '\\0'; // Remove newline character
                  
                  // Ensure searchStr and replaceStr are within the allowed length
                  if (strlen(searchStr) > 10 || strlen(replaceStr) > 10) {
                      fprintf(stderr, "Search and replacement strings must be up to 10 characters long.\\n");
                      return 1;
                  }
                  
                  // Process
                  
                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #10

                  Member 16311074 wrote:

                  replaced = 0; // Reset for the next string

                  Since replaced is a local variable (and searchAndReplace() is not static), this statement does nothing useful. This has nothing to do with your overall issue, though.

                  "One man's wage rise is another man's price increase." - Harold Wilson

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                  1 Reply Last reply
                  0
                  • M Member_16311074

                    the debugger let me input my search string and not letting me enter my replacement string. I made it so that 10 letters can be enter into it but it stop working when I put letters over 3. Are there any chance the program is mistaking one japanese word into 3 letters?

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #11

                    Member 16311074 wrote:

                    ...not letting me enter my replacement string.

                    Try changing the size of searchStr and replaceStr to something too large (but still only enter 10 characters). When you make them just large enough and you enter in that many characters, fgets() leaves the \n in the keyboard buffer and the second call to fgets() see it and exits right away.

                    "One man's wage rise is another man's price increase." - Harold Wilson

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                    1 Reply Last reply
                    0
                    • M Member_16311074

                      the debugger let me input my search string and not letting me enter my replacement string. I made it so that 10 letters can be enter into it but it stop working when I put letters over 3. Are there any chance the program is mistaking one japanese word into 3 letters?

                      R Offline
                      R Offline
                      RedDk
                      wrote on last edited by
                      #12

                      First run: ~~~~~~~~~~~~~~~~ Original strings: ∩╝æ∩╝ÿµ¡│πü¿∩╝ÿ∩╝浡│πü«ΘüòπüäπÇé∩╝æ∩╝ÿµ¡│πü»µüïπü½µ║║πéîπéïΣ║║πüºπÇü∩╝ÿ∩╝浡│πü»πüèΘó¿σæéπü½µ║║πéîπéïΣ║║πÇé∩╝æ∩╝ÿµ¡│πü»ΘüôπéÆΦ╡░πéèπÇüΘÇåπééπü╛πüƒτä╢πéèπÇé∩╝ÿ∩╝浡│πü«Σ║║πüîΦ╡░πüúπüªπüäπéïπÇé∩╝æ∩╝ÿµ¡│πü»σ┐âπüîπééπéìπüÅπÇü∩╝ÿ∩╝浡│πü»Θ¬¿πüîπééπéìπüäπÇéτºüπü»∩╝æ∩╝ÿµ¡│πüºσ┐âΦçôπüóπü╛πéëπü¬πüäπÇé ∩╝ÿ∩╝浡│πü«σ╜╝πü»Θú¢Φíîµ⌐ƒπ鯵¡óπéüπéëπéîπü¬πüäπÇé∩╝æ∩╝ÿµ¡│πü»µüïπü½τ¬Æµü»πüÖπéïπüôπü¿πüîπüºπüìπÇü∩╝ÿ∩╝浡│πü»Θñàπü½τ¬Æµü»πüÖπéïπüôπü¿πüîπüºπüìπéïπÇéσüÅσ╖«σÇñπüîµ░ùπü½πü¬πéïπÇéτºüπü»∩╝æ∩╝ÿµ¡│πüºπÇü∩╝ÿ∩╝浡│πüºΦíÇσ£ºπü¿ΦíÇτ│ûσÇñπ鯵░ùπü½πüùπüªπüäπéïπÇéπü╛πüáΣ╜òπééτƒÑπéëπü¬πüä∩╝æ∩╝ÿµ¡│πÇüπééπüåΣ╜òπééΦªÜπüêπüªπüäπü¬πüä∩╝ÿ∩╝浡│πÇé Φç¬σêåπ鯵Äóπüùπüªπüäπéï∩╝æ∩╝ÿµ¡│πü¿πÇüπü┐πéôπü¬π鯵Äóπüùπüªπüäπéï∩╝ÿ∩╝浡│πÇé Enter search string (up to 10 characters): 2 Enter replacement string (up to 10 characters): # Nothing is changed. Second run: ~~~~~~~~~~~ Original strings: ∩╝æ∩╝ÿµ¡│πü¿∩╝ÿ∩╝浡│πü«ΘüòπüäπÇé∩╝æ∩╝ÿµ¡│πü»µüïπü½µ║║πéîπéïΣ║║πüºπÇü∩╝ÿ∩╝浡│πü»πüèΘó¿σæéπü½µ║║πéîπéïΣ║║πÇé∩╝æ∩╝ÿµ¡│πü»ΘüôπéÆΦ╡░πéèπÇüΘÇåπééπü╛πüƒτä╢πéèπÇé∩╝ÿ∩╝浡│πü«Σ║║πüîΦ╡░πüúπüªπüäπéïπÇé∩╝æ∩╝ÿµ¡│πü»σ┐âπüîπééπéìπüÅπÇü∩╝ÿ∩╝浡│πü»Θ¬¿πüîπééπéìπüäπÇéτºüπü»∩╝æ∩╝ÿµ¡│πüºσ┐âΦçôπüóπü╛πéëπü¬πüäπÇé ∩╝ÿ∩╝浡│πü«σ╜╝πü»Θú¢Φíîµ⌐ƒπ鯵¡óπéüπéëπéîπü¬πüäπÇé∩╝æ∩╝ÿµ¡│πü»µüïπü½τ¬Æµü»πüÖπéïπüôπü¿πüîπüºπüìπÇü∩╝ÿ∩╝浡│πü»Θñàπü½τ¬Æµü»πüÖπéïπüôπü¿πüîπüºπüìπéïπÇéσüÅσ╖«σÇñπüîµ░ùπü½πü¬πéïπÇéτºüπü»∩╝æ∩╝ÿµ¡│πüºπÇü∩╝ÿ∩╝浡│πüºΦíÇσ£ºπü¿ΦíÇτ│ûσÇñπ鯵░ùπü½πüùπüªπüäπéïπÇéπü╛πüáΣ╜òπééτƒÑπéëπü¬πüä∩╝æ∩╝ÿµ¡│πÇüπééπüåΣ╜òπééΦªÜπüêπüªπüäπü¬πüä∩╝ÿ∩╝浡│πÇé Φç¬σêåπ鯵Äóπüùπüªπüäπéï∩╝æ∩╝ÿµ¡│πü¿πÇüπü┐πéôπü¬π鯵Äóπüùπüªπüäπéï∩╝ÿ∩╝浡│πÇé Enter search string (up to 10 characters): πéèπ Enter replacement string (up to 10 characters): ---- ∩╝æ∩╝ÿµ¡│πü¿∩╝ÿ∩╝浡│πü«ΘüòπüäπÇé∩╝æ∩╝ÿµ¡│πü»µüïπü½µ║║πéîπéïΣ║║πüºπÇü∩╝ÿ∩╝浡│πü»πüèΘó¿σæéπü½µ║║πéîπéïΣ║║πÇé∩╝æ∩╝ÿµ¡│πü»ΘüôπéÆΦ╡░πéèπÇüΘÇåπééπü╛πüƒτä╢πéèπÇé∩╝ÿ∩╝浡│πü«Σ║║πüîΦ╡░πüúπüªπüäπéïπÇé∩╝æ∩╝ÿµ¡│πü»σ┐âπüîπééπéìπüÅπÇü∩╝ÿ∩╝浡│πü»Θ¬¿πüîπééπéìπüäπÇéτºüπü»∩╝æ∩╝ÿµ¡│πüºσ┐âΦçôπüóπü╛πéëπü¬πüäπÇé ∩╝æ∩╝ÿµ¡│πü¿∩╝ÿ∩╝浡│πü«ΘüòπüäπÇé∩╝æ∩╝ÿµ¡│πü»µüïπü½µ║║πéîπéïΣ║║πüºπÇü∩╝ÿ∩╝浡│πü»πüèΘó¿σæéπü½µ║║πéîπéïΣ║║πÇé∩╝æ∩╝ÿµ¡│πü»ΘüôπéÆΦ╡░----ÇüΘÇåπééπü╛πüƒτä╢----Çé∩╝ÿ∩╝浡│πü«Σ║║πüîΦ╡░πüúπüªπüäπéïπÇé∩╝æ∩╝ÿµ¡│πü»σ┐âπüîπééπéìπüÅπÇü∩╝ÿ∩╝浡│πü»Θ¬¿πüîπééπéìπüäπÇéτºüπü»∩╝æ∩╝ÿµ¡│πüºσ┐âΦçôπüóπü╛πéëπü¬πüäπÇé Seems to be running as expected here.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups