#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    How to display palindromes in a given randomized letters


    hello everyone, can anybody help me how to count and display the palindromes
    in this randomized letters:

    this is my program but it only prints randomized letters and can't count the palindromes words and display it:

    Code:
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int max=0; 
    int copy(char *str,int n);
    int rev(char *s);
    int palind(char *s);
    
    
    
    
    main(){
       int x;
       int y[50];
       int i=0;
    
    
    
    
       clrscr();
    
       for(i=0; i<200; i++){
       srand(time(NULL));
    
     for(i=0;i<=200;i++){
    
     x =rand()%26+97;
     y[i] = x%26+97;
     printf("%2c", x);
     i++;
     }
     }
    
    
      for(i=0; i<200; i++){
       srand(time(NULL));
    
     for(i=0;i<=200;i++){
    
     x =rand()%26+97;
     y[i] = x%26+97;
     printf("%2c", x);
     i++;
     }
     }
      for(i=0; i<200; i++){
       srand(time(NULL));
    
     for(i=0;i<=200;i++){
    
     x =rand()%26+97;
     y[i] = x%26+97;
     printf("%2c", x);
     i++;
     }
     }
      for(i=0; i<200; i++){
       srand(time(NULL));
    
     for(i=0;i<=200;i++){
    
     x =rand()%26+97;
     y[i] = x%26+97;
     printf("%2c", x);
     i++;
     }
     }
      for(i=0; i<200; i++){
       srand(time(NULL));
    
     for(i=0;i<=200;i++){
    
     x =rand()%26+97;
     y[i] = x%26+97;
     printf("%2c", x);
     i++;
     }
     }
      for(i=0; i<200; i++){
       srand(time(NULL));
    
     for(i=0;i<=200;i++){
    
     x =rand()%26+97;
     y[i] = x%26+97;
     printf("%2c", x);
     i++;
     }
     }
      for(i=0; i<200; i++){
       srand(time(NULL));
    
     for(i=0;i<=200;i++){
    
     x =rand()%26+97;
     y[i] = x%26+97;
     printf("%2c", x);
     i++;
     }
     }
     
    
     printf("\n\nThe PALINDROMES are:\n");
     palind(x); 
     printf("The number of Palindrome is %d",max); 
     getchar(); 
    
    }
    
    
    int copy(char *str,int n) { 
    
    char *st; 
    int i,q; 
     st=malloc(sizeof(char)*n); 
     for(i=0;i<=n;i++){ 
      st[i]=str[i]; 
     } 
     st[n+1]='\0'; 
     x=strlen(st);
     if(q>=3&&st[0]!=' '){
     printf("%s\n",st);
     max++;
     }
    
    } 
    
    
    int rev(char *s) { 
    int i,k,r,len; 
    len=strlen(s); 
     for(i=len-1;i>0;i--){ 
      k=0; 
      r=i; 
      while(k < r && s[k] == s[r]){ 
       k++; 
       r--; 
      } 
    
      if(k >= r){ 
       copy(s,i); 
      } 
     } 
    }  
    
    int palind(char *s) { 
    int i,len; 
    len=strlen(s); 
     for(i=0;i<len;i++){ 
      rev(&s[i]); 
     } 
     
     getch();
    
    }
    
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    Code:
      int y[50]; // A
      int i=0;                  // F
      srand(time(NULL));
      for(i=0; i<200; i++){         // B
        for(i=0;i<=200;i++){     // C
          x =rand()%26+97;        // G
          y[i] = x%26+97;         // D
          printf("%2c", x);
          i++;                  // E
        }
      }
    Line F you sent i to 0. Then you repeat that on lines B and C. The outer loop starts on B. The inner loop on C. On line C and E you increment i. The inner loop runs 201 times leaving i with the value 201. The outer loop is finished.

    We get rid of the outer loop B because it's ridiculous. Lines G and D give you a random lower case letter. Why do you repeat the computation? But of course, what actually happens is unknown because you overwrite memory. On line A y is declared to store 50 characters. Within the inner loop you fill 201 positions, overwriting whatever memory determined by however your compiler layed out memory.

    And then you repeat this nasty code seven or six or eight times. Why???

    Then over in copy you've allocated space for n characters but fill the memory with n+2 characters.

    Counting lessons. Let n be 2.

    0
    1
    n
    n+1

    You see? n+2 values stored.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo