patterncMajor
Palindromes in C
Viewed 0 times
palindromesstackoverflowprogramming
Problem
The function tests whether or not the provided string is a palindrome, and the
header.h
main.c
Output is about 0.016.
Is this as fast as it gets?
main function just times how quick it is (after all, C's supposed to be quick, right?).header.h
bool palindrome(char *text);main.c
#include
#include
#include
#include
#include "header.h"
int main() {
clock_t start;
start = clock();
int j;
for(j = 0; j < 1000000; j++) {
palindrome("racecar");
}
printf("%.10f", (double) (clock() - start) / CLOCKS_PER_SEC);
return 0;
}
bool palindrome(char *text) {
char *right = text + strlen(text);
while(text < --right) {
if(*text++ != *right) {
return false;
}
}
return true;
}Output is about 0.016.
Is this as fast as it gets?
Solution
A few notes:
-
Put some include guards in your header.
-
Name your header something more unique and appropriate.
-
Always declare what parameters your function takes in, even if nothing.
You might wonder why we have to do this. Imagine we have the function
In C, this is known as an identifier list and means that it "can take any number of parameters of unknown types". We can actually pass values to the function even though we don't mean to or intend to. If the caller calls the function giving it some argument, the behavior is undefined. The stack could become corrupted for example, because the called function expects a different layout when it gains control.
Using identifier lists in function parameters is deprecated. It is much better to do something like:
In C, this is known as a parameter type list and defines that the function takes zero arguments (and also communicates that when reading it) - like with all cases where the function is declared using a parameter type list, which is called a prototype. If the caller calls the function and gives it some argument, that is an error and the compiler spits out an appropriate error.
The second way of declaring a function has plenty of benefits. One of course is that amount and types of parameters are checked. Another difference is that because the compiler knows the parameter types, it can apply implicit conversions of the arguments to the type of the parameters. If no parameter type list is present, that can't be done, and arguments are converted to promoted types (that is called the default argument promotion).
-
I'm not sure I'd have the timing implemented in as part of your code, instead I'd use the bash command
-
Notice that you can collapse your
You could also transition from pointer usage to array usage as @JS1 has in his code example, something I would probably recommend as a beginner. Note that if you do transition to arrays, this may come with a small performance hit (depending on compiler optimizations).
-
You don't need the
-
Put some include guards in your header.
-
Name your header something more unique and appropriate.
-
Always declare what parameters your function takes in, even if nothing.
int main(void)You might wonder why we have to do this. Imagine we have the function
foo() declared as such:int foo()In C, this is known as an identifier list and means that it "can take any number of parameters of unknown types". We can actually pass values to the function even though we don't mean to or intend to. If the caller calls the function giving it some argument, the behavior is undefined. The stack could become corrupted for example, because the called function expects a different layout when it gains control.
Using identifier lists in function parameters is deprecated. It is much better to do something like:
int foo(void)In C, this is known as a parameter type list and defines that the function takes zero arguments (and also communicates that when reading it) - like with all cases where the function is declared using a parameter type list, which is called a prototype. If the caller calls the function and gives it some argument, that is an error and the compiler spits out an appropriate error.
The second way of declaring a function has plenty of benefits. One of course is that amount and types of parameters are checked. Another difference is that because the compiler knows the parameter types, it can apply implicit conversions of the arguments to the type of the parameters. If no parameter type list is present, that can't be done, and arguments are converted to promoted types (that is called the default argument promotion).
char will become int, for example, while float will become double.-
I'm not sure I'd have the timing implemented in as part of your code, instead I'd use the bash command
time as such to time it (please note that this output has no relation to your program and it is just an example):$ time ./a.out
real 0m0.007s
user 0m0.004s
sys 0m0.005s-
Notice that you can collapse your
palindrome() function from the while() loop into a for loop:bool palindrome(char *text)
{
for(char *right = text + strlen(text) - 1; text < right; text++, right--)
{
if(*text != *right)
{
return false;
}
}
return true;
}You could also transition from pointer usage to array usage as @JS1 has in his code example, something I would probably recommend as a beginner. Note that if you do transition to arrays, this may come with a small performance hit (depending on compiler optimizations).
-
You don't need the
return 0; at the end of main, per the C standard.Code Snippets
int main(void)int foo(void)$ time ./a.out
real 0m0.007s
user 0m0.004s
sys 0m0.005sbool palindrome(char *text)
{
for(char *right = text + strlen(text) - 1; text < right; text++, right--)
{
if(*text != *right)
{
return false;
}
}
return true;
}Context
StackExchange Code Review Q#112401, answer score: 24
Revisions (0)
No revisions yet.