Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int is_palindrome(const char* str, size_t length){
- int i = 0;
- int j = length - 1;
- for(;j>=i && *(str +i) == *(str+j);i++,j--);
- return j <= i;
- }
- void palindrome_by_line(const char* str){
- unsigned current_length;
- int current_line = 1;
- int amount;
- char* inner_str;
- char* inner_inner_str;
- //controls the whole string
- while(*str){
- while(*(str) && *(str) == ' ') str++;
- inner_str = (char*)str;
- //controls until line feed
- amount =0;
- while((*inner_str) && *(inner_str) != '\n'){
- inner_inner_str = inner_str;
- //controls until space is found
- current_length = 0;
- while((*inner_inner_str) && (*inner_inner_str) != '\n' && *(inner_inner_str) != ' '){
- current_length++;
- inner_inner_str++;
- }
- if(is_palindrome(inner_inner_str - current_length , (inner_inner_str - inner_str)))
- amount++;
- while(*(inner_inner_str) && *(inner_inner_str) == ' ') inner_inner_str++;
- inner_str += (inner_inner_str - inner_str);
- }
- printf("Cantidad de palindromos en esta linea %d: %d\n", current_line, amount);
- current_line++;
- if(*inner_str == '\n')
- str += (inner_str - str) + 1;
- else
- str += (inner_str - str);
- }
- }
- int main(void) {
- palindrome_by_line(" menem 121\n radar\nneuquen reconocer\nanitalavalatina\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement