fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define BUFFER_SIZE 1024
  5.  
  6. void convert(const char *input) {
  7. char buffer[BUFFER_SIZE];
  8. int index = 0;
  9. int ch;
  10.  
  11. // Simulate reading from input string
  12. for (int i = 0; i < strlen(input); i++) {
  13. ch = input[i]; // Read a character from the input string
  14.  
  15. if (ch == '\0') { // Check for null terminator
  16. break;
  17. }
  18.  
  19. if (ch == '\n') { // Check for newline
  20. buffer[index] = '\0'; // Null-terminate the string
  21. printf("Processed line: %s\n", buffer); // Print the processed line
  22. index = 0; // Reset index for the next line
  23. continue;
  24. }
  25.  
  26. if (index < BUFFER_SIZE - 1) { // Ensure we don't exceed buffer size
  27. buffer[index++] = ch; // Store character in buffer
  28. } else {
  29. buffer[index] = '\0'; // Null-terminate the string
  30. printf("Buffer full, processed line: %s\n", buffer); // Print the processed line
  31. index = 0; // Reset index for the next line
  32. }
  33.  
  34. if (ch == 'x') { // Check for 'x'
  35. printf("Found 'x' at index %d\n", index - 1); // Print index of 'x'
  36. }
  37. }
  38.  
  39. // Check if there's any remaining data in the buffer after processing
  40. if (index > 0) {
  41. buffer[index] = '\0'; // Null-terminate the string
  42. printf("Processed line: %s\n", buffer); // Print the processed line
  43. }
  44. }
  45.  
  46. int main() {
  47. const char *input = "1020\n"; // Input string containing the number 1020
  48. printf("Processing input: %s", input);
  49. convert(input); // Call the convert function with the input string
  50. return 0; // Return 0 upon completion
  51. }
Success #stdin #stdout 0s 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
Processing input: 1020
Processed line: 1020