Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5.  
  6. int * removeSpace(char* str) {
  7.  
  8.     char *out = str;
  9.     char *put = str;
  10.  
  11.     for(; *str != '\0'; ++str)
  12.     {
  13.         if(*str != ' ')
  14.         *put++ = *str;
  15.     }
  16.     *put = '\0';
  17.  
  18.   return out;
  19. }
  20.  
  21. int string_length(char *string)
  22. {
  23.    int length = 0;
  24.  
  25.    while(*string)
  26.    {
  27.       length++;
  28.       string++;
  29.    }
  30.  
  31.    return length;
  32. }
  33.  
  34. void reverses(char* reverse){
  35.    int length, c;
  36.    char *begin, *end, temp;
  37.  
  38.    length = string_length(reverse);
  39.  
  40.    begin = reverse;
  41.    end = reverse;
  42.  
  43.    for ( c = 0 ; c < ( length - 1 ) ; c++ )
  44.        end++;
  45.  
  46.    for ( c = 0 ; c < length/2 ; c++ )
  47.    {
  48.       temp = *end;
  49.       *end = *begin;
  50.       *begin = temp;
  51.  
  52.       begin++;
  53.       end--;
  54.    }
  55. }
  56.  
  57. void lower_string(char* str) {
  58.    int c = 0;
  59.  
  60.    while (str[c] != '\0') {
  61.       if (str[c] >= 'A' && str[c] <= 'Z') {
  62.          str[c] = str[c] + 32;
  63.       }
  64.       c++;
  65.    }
  66. }
  67.  
  68. void copy_string(char *target, char *source)
  69. {
  70.    while(*source)
  71.    {
  72.       *target = *source;
  73.       source++;
  74.       target++;
  75.    }
  76.    *target = '\0';
  77. }
  78.  
  79. int compare_string(char *first, char *second)
  80. {
  81.    while(*first==*second)
  82.    {
  83.       if ( *first == '\0' || *second == '\0' )
  84.          break;
  85.  
  86.       first++;
  87.       second++;
  88.    }
  89.    if( *first == '\0' && *second == '\0' )
  90.       return 0;
  91.    else
  92.       return -1;
  93. }
  94.  
  95. int checkForPalindrom(char* string){
  96.  
  97.     int check, length;
  98.     char *reverse;
  99.     lower_string(string);
  100.     length = string_length(string);
  101.     reverse = (char*)malloc(length+1);
  102.  
  103.     copy_string(reverse,string);
  104.     reverses(reverse);
  105.     removeSpace(string);
  106.  
  107.     check = compare_string(string, reverse);
  108.  
  109.     free(reverse);
  110.  
  111.     if(check == 0)
  112.         return 1;
  113.     else
  114.         return 0;
  115.  
  116. }
  117.  
  118. int main(void)
  119. {
  120.         char palindrom[100];
  121.         bool quit = false;
  122.         int result;
  123.         char answer;
  124.         while(!quit){
  125.  
  126.  
  127.  
  128.         printf("Skriv in en mening: ");
  129.         //scanf(" %c")
  130.         //scanf(" %[^\n]%*s", palindrom);
  131.         gets(palindrom);
  132.  
  133.         result = checkForPalindrom(palindrom);
  134.  
  135.  
  136.         if ( result == 1 )
  137.             printf("\"%s\" is a palindrome string.\n", palindrom);
  138.         else
  139.             printf("\"%s\" is not a palindrome string.\n", palindrom);
  140.  
  141.         memset(palindrom, 0, 10);
  142.  
  143.         printf("\nWould you like to start over? (j/n): ");
  144.         scanf("%s", &answer);
  145.  
  146.         if(answer == 'j')
  147.             continue;
  148.         else if(answer == 'n'){
  149.             quit = true;
  150.         }
  151.  
  152.  
  153.  
  154.         }
  155.  
  156.  
  157.         return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement