Software-OK
≡... News | ... Home | ... FAQ | Impressum | Contact | Listed at | Thank you |

  
HOME ► Faq ► FAQ - Windows-CPP-und-C ► ««« »»»

Find and replace words in clean C program?


Searching and replacing words or substrings in text is not necessarily simple or difficult in C!





1. Search and Replace in a C Program:
2. Tips for Searching and Replacing Words or Substrings in Text:









1.) Search and Replace in a C Program:



Here is a revised version of the code with English identifiers and commented sections:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// Function to search and replace substrings
void searchAndReplace(char *source, char *search, char *replace, char *result) {
  int i, j, k;
  int sourceLength = strlen(source);
  int searchLength = strlen(search);
  int replaceLength = strlen(replace);

  char* sourceCopy = source;  // Copy of the source string
  char* searchCopy = search;  // Copy of the search string

  for (i = 0, j = 0; i < sourceLength; ) {
      if (sourceCopy[i] == searchCopy[0]) {
          // Check if the substring matches
          int match = 1;
          for (k = 1; k < searchLength; k++) {
              if (sourceCopy[i + k] != searchCopy[k]) {
                  match = 0;
                  break;
              }
          }
          if (match) {
              // Substring found, replace it
              for (k = 0; k < replaceLength; k++) {
                  result[j++] = replace[k];
              }
              i += searchLength;
          } else {
              // No match, copy and continue
              result[j++] = sourceCopy[i++];
          }
      } else {
          // No match, copy and continue
          result[j++] = sourceCopy[i++];
      }
  }

  result[j] = "\0"; // Null-terminate the result
}

int main() {
  char *source = "Hello World! Hello, Universe!";
  char *search = "Hello";
  char *replace = "Hi";

  char result[100]; // Assumption: Result fits in the buffer

  searchAndReplace(source, search, replace, result);

  printf("Original: %s\n", source);
  printf("Replaced: %s\n", result);

  printf("Waiting for a key press.\n");

  // Wait for key press (Windows-specific)
  _getch();

  return 0;
}

The above version of the code uses English identifiers and includes commented sections to explain how the code works. Note that some comments are kept in German for clarity.

2.) Tips for Searching and Replacing Words or Substrings in Text:



Searching and replacing words or substrings in text in C is not necessarily "easy" or "hard". Rather, it depends on the complexity of the requirements and the available tools. The code shown solves the task in a relatively straightforward manner. Here are some reasons why it seems so simple:

- Simple Requirements:
The code implements basic search and replace logic for a static set of strings. Complex requirements such as regular expressions, performance optimizations, or multibyte text processing are not considered.

- Use of Standard Functions:
The code uses standard C functions like strlen, tolower, strcpy, and loops for character processing. These functions are simple and generally available.

- Simple Character Processing:
The code uses a straightforward loop to scan and process the source code. It checks character by character for matches and performs replacements.

- Lack of Error Handling:
The shown code does not handle errors, such as the case where the source string does not provide enough space for the result. This makes the code simpler but not recommended for robust programs.

It's important to note that searching and replacing strings in a real-world production program can be much more complex, especially when dealing with complex requirements, international character sets, performance, and error handling. There are also libraries and frameworks that provide advanced text processing functions to make such tasks simpler and more efficient.






FAQ 40: Updated on: 4 September 2024 11:21 Windows
Windows-CPP-und-C

Using arrays and strings in C programs!


This C program explains and demonstrates some important concepts related to the use of arrays and strings. Lets walk through the different aspects of this
Windows-CPP-und-C

Rename files in C++ with wildcards across directories?


Renaming Files in C++ Across Directories with Placeholders, and the Simplicity of Renaming Files in Windows 1. Renaming Files Across Directories with Placeholders
Windows-CPP-und-C

Determine if my program is running in the active season, CPP on Windows?


It’s Not Quite Easy to Determine If Your C or C++ Program is Running in the Active Session, But It’s Not Impossible Here’s the Solution 1. Is My C++ or
Windows-CPP-und-C

What is bufferoverflowU.lib and when is it needed?


The  bufferoverflowU.lib  is a static library provided by Microsoft to improve protection against buffer overflows in programs built with older versions
Windows-CPP-und-C

In Visual Studio there is a search for wildcards and regular expressions?


But whats the difference? Visual Studio has both wildcard searches and regular expression searches, and they serve different purposes: 1. Wildcard search:
Windows-CPP-und-C

Why does it take forever to compile in VS 2022, what can I do?


If compiling in Visual Studio 2022, 2019, 2017, etc. takes an unusually long time, there could be several reasons. Here are some steps you can try to speed
Windows-CPP-und-C

What is the latest / current C++ version?


The last stable version of C++ is C++23. It was officially released in 2023. This version brings numerous improvements and new features, including: 1. The

»»

  My question is not there in the FAQ
Asked questions on this answer:
Keywords: cpp, windows, find, replace, words, clean, program, searching, replacing, substrings, text, necessarily, simple, difficult, search, tips, Questions, Answers, Software




  

  + Freeware
  + Order on the PC
  + File management
  + Automation
  + Office Tools
  + PC testing tools
  + Decoration and fun
  + Desktop-Clocks
  + Security

  + SoftwareOK Pages
  + Micro Staff
  + Freeware-1
  + Freeware-2
  + Freeware-3
  + FAQ
  + Downloads

  + Top
  + Desktop-OK
  + The Quad Explorer
  + Don't Sleep
  + Win-Scan-2-PDF
  + Quick-Text-Past
  + Print Folder Tree
  + Find Same Images
  + Experience-Index-OK
  + Font-View-OK


  + Freeware
  + IsMyLcdOK
  + DesktopDigitalClock
  + ClassicDesktopClock
  + PreventTurnOff
  + QuickMemoryTestOK
  + IsMyHdOK
  + AutoPowerOptionsOK
  + OneLoupe
  + TraceRouteOK
  + PAD-s


Home | Thanks | Contact | Link me | FAQ | Settings | Windows 10 | gc24b | English-AV | Impressum | Translate | PayPal | PAD-s

 © 2025 by Nenad Hrg softwareok.de • softwareok.com • softwareok.com • softwareok.eu


► Local Security Police account lockout duration on MS Windows OS! ◄
► Close correctly, the command prompt e.g. cmd.exe! ◄
► Convert the table into an image in Microsoft Word? ◄
► How can I see the Windows command line history in the cmd.exe? ◄


This website does not store personal data. However, third-party providers are used to display ads,
which are managed by Google and comply with the IAB Transparency and Consent Framework (IAB-TCF).
The CMP ID is 300 and can be individually customized at the bottom of the page.
more Infos & Privacy Policy

....