Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.18 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define MAXCHRS 5
  5. #define MAXTEXT 200
  6.  
  7. void inline clearbuffer()
  8. {
  9.     while (getchar() != '\n');
  10.     return;
  11. }
  12.  
  13. char doreplace(char chr, char *search, char *replace)
  14. {
  15.     char *search_tmp;
  16.     if(search_tmp=strchr(search,chr))
  17.     {
  18.         return *(search_tmp-search+replace);
  19.     }
  20.    
  21.     return chr;
  22. }
  23.  
  24. int getline(char *query, char *str, int num)
  25. {
  26.     char chr, *str_bak = str;
  27.     printf("%s (%d ZEICHEN)\n", query,num);
  28.     printf("S: ");
  29.     while (666)
  30.     {
  31.         memset(str,0,num-1);
  32.         while (( chr=getchar() )!='\n')
  33.         {
  34.             if (str>=str_bak+num)
  35.             {
  36.                 printf("*** MAXIMAL %d ZEICHEN ERLAUBT ***\nS: ", num);
  37.                 str=str_bak;
  38.                 clearbuffer();
  39.                 break;
  40.             }
  41.             *str=chr;
  42.             str++;
  43.         }
  44.  
  45.         if (!(str==str_bak))
  46.         {
  47.             str=str_bak;
  48.             return strlen(str);
  49.         }
  50.     }
  51. }
  52.  
  53.  
  54. int main()
  55. {
  56.  
  57.     char search[MAXCHRS+1], repl[MAXCHRS+1], text[MAXTEXT+1], *tmp;
  58.     getline("SUCHEN",search, MAXCHRS);
  59.     do
  60.     {
  61.         getline("ERSETZEN",repl, MAXCHRS);
  62.     }
  63.     while( strlen(search)!=strlen(repl) );
  64.     getline("TEXT",text,MAXTEXT);
  65.     tmp=text;
  66.     printf("   ");
  67.     while(*tmp)
  68.     {
  69.         putchar(doreplace(*tmp,search,repl));
  70.         tmp++;
  71.     }
  72.     putchar('\n');
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement