fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <regex>
  6.  
  7. using namespace std;
  8.  
  9. string replaceWord(const string& text, const string& wordToReplace, const string& replacement) {
  10. regex word_regex("\\b" + wordToReplace + "\\b");
  11. return regex_replace(text, word_regex, replacement);
  12. }
  13.  
  14. int main() {
  15. string code;
  16. string wordToReplace;
  17. string fileFormat;
  18. vector<string> replacementWords;
  19.  
  20. // Get the code
  21. cout << "Enter your code (end with a line containing only 'END'):\n";
  22. string line;
  23. while (getline(cin, line) && line != "END") {
  24. code += line + "\n";
  25. }
  26.  
  27. // Get the word to replace
  28. cout << "Enter word to replace: ";
  29. getline(cin, wordToReplace);
  30.  
  31. // Get replacement words
  32. cout << "Enter replacement words (one per line, end with a line containing only 'END'):\n";
  33. while (getline(cin, line) && line != "END") {
  34. if (!line.empty()) {
  35. replacementWords.push_back(line);
  36. }
  37. }
  38.  
  39. // Get file format
  40. cout << "Enter file format (txt/xpm): ";
  41. getline(cin, fileFormat);
  42.  
  43. // Generate and save variations
  44. for (size_t i = 0; i < replacementWords.size(); i++) {
  45. string variation = replaceWord(code, wordToReplace, replacementWords[i]);
  46. string filename = "variation_" + to_string(i + 1) + "." + fileFormat;
  47.  
  48. ofstream outFile(filename);
  49. if (fileFormat == "xpm") {
  50. outFile << "XPM1\n";
  51. }
  52. outFile << variation;
  53. outFile.close();
  54.  
  55. cout << "Saved: " << filename << "\n";
  56. }
  57.  
  58. cout << "\nCreated " << replacementWords.size() << " variations!\n";
  59. return 0;
  60. }
Success #stdin #stdout 0.01s 5288KB
stdin
/*  Berechnung des Hamming-Abstandes zwischen zwei 128-Bit Werten in 	*/
/*	einer Textdatei. 													*/
/*  Die Werte müssen auf einer separaten Zeile gespeichert sein			*/
/* 																		*/
/*	Erstellt: 17.5.2010													*/
/*  Autor: Thomas Scheffler												*/

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

#define ARRAY_SIZE 32

unsigned Hamdist(unsigned x, unsigned y)
{
  unsigned dist = 0, val = x ^ y;
 
  // Count the number of set bits
  while(val)
  {
    ++dist; 
    val &= val - 1;
  }
 
  return dist;
}



int main (void)
{
	char hex;
	int i;
	int a[ARRAY_SIZE];
	int b[ARRAY_SIZE];
	int hamDist = 0;
	FILE* fp;
	
	//Arrays mit 0 initialisieren
	for (i = 0; i < ARRAY_SIZE; ++i)
	{
  		a[i] = 0;
  		b[i] = 0;
	}

	
	fp = fopen("hex.txt","r");
	if (fp == NULL) 
	{
		printf("Die Datei hex.txt wurde nicht gefunden!");
		exit(EXIT_FAILURE);
	}

	i=0;
	printf("1.Zeile einlesen.\n");

 	while((hex=fgetc(fp))!='\n' && hex != EOF)
    {
        a[i]=strtol(&hex,0,16);
		i++;
    }
	i=0;
	printf("2.Zeile einlesen.\n");

 	while((hex=fgetc(fp))!='\n' && hex != EOF)
    {
    	b[i]=strtol(&hex,0,16);
        i++;
    }
	fclose(fp);

	printf("Hamming-Abweichung pro Nibble:\n");
	for (i = 0; i < ARRAY_SIZE; ++i)
	{
		printf ("%i\t%i\t%i\n",a[i],b[i],Hamdist(a[i],b[i]));
		hamDist += Hamdist(a[i],b[i]);
	}
	printf ("\nHamming-Abweichung der Hash-Werte:%d\n",hamDist);
}

stdout
Enter your code (end with a line containing only 'END'):
Enter word to replace: Enter replacement words (one per line, end with a line containing only 'END'):
Enter file format (txt/xpm): 
Created 0 variations!