Shortest C code to reverse a string [closed] - c

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
Not counting the function signature (just the body) can anybody produce C code shorter than this function that will reverse a string and return the result as a pointer to the reversed string.. (not using a string reverse library function either)?
char * reverse_str(char * s)
{
char c,*f=s,*p=s;while(*p)p++;while(--p>s){c=*p;*p=*s;*s++=c;}return f;
}

not much longer, but it works.
#include <string.h>
/* precondition: s!=const && s!=NULL && *s!='\0' */
char *mystrrev(char *s)
{
char *a=s,*e=s+strlen(s)-1;
while( a<e )
{
char c=*a;
*a++=*e;
*e--=c;
}
return s;
}

Related

Extracting bytes from a byte array one by one (C) [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a byte array (dB). I am trying to extract the bytes one by one. Why isn't this code working? Any pointers? Logically am I wrong? Or something wrong with my implementation?
You have byte buffers declared like this:
unsigned char *decodeBuf;
To read a single value from that buffer, at offset i you simply write:
unsigned char b = decodeBuf[i];
try
int main()
{
unsigned char tmp;
tmp = getByte(dB+dOffset); dOffset++;
}
it should work

C to MIPS convert [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Can anyone help me convert the C code below to MIPS code? I'm supposed to use recursion and get 21 - 2 as the final answer. Thanks!
/*
x is a pointer to a linked list node and is not null.
Return the min value stored in the linked list.
Assume each node is stored in memory as an int (value) followed by a pointer to the next node (next), each a word wide.
You must write it with recursion. */
int findMin(node *x) {
if(x->next == NULL)
return x->value;
else {
int min = findMin(x->next);
if(min < x->value)
return min;
else
return x->value;
}
}
Here you are:
mips-linux-gnu-gcc -S -o foo.asm foo.c

How to convert a string containing a hex character code to the character value? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have an array of chars "0x55".
What I want to do is convert it to a char which is going to be U (because ASCII 0x55 = U).
So how to do this conversion?
#include <windows.h>
int main()
{
array[] = "0x55"
char test;
**// I want to move the string to that test to be one character which is U**
}
Any suggestions?
I think this is what you are after:
int main(int argc,char**argv)
{
char array[] = "0x55";
int value;
char test;
sscanf(array,"%x",&value);
test = value;
return 0;
}
In C++, I would code it a little differently, but this seems more like a C question.

an efficent version similar to strstr [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
here is a general implementation
int stridx (char[] src, char[] str){
int i,j,k;
for(i=0;i < (src.len - str.len);i++){
for(j=i,k=0; str[k] != '\0' && str[k] == src[i]; j++,k++);
if( k> 0 && str[k]=='\0') return i;
}
return -1;
}
The worst case of the algorithm could be n^2, if we have aaaaaaaaaaaaaaaaaaaaaaaa (assuming both src and str are very long, and the length of them is very close).
Can I have a better algorithm?
You could use the Boyer-Moore algorithm, which is O(n). Here's sample C code.
It's not necessary to calculate the length of the strings :
char *strr(char *s1,char *s2)
{
int i,j;
for(i=0;s1[i];i++)
if(s1[i]==s2[0])
for(j=0;s1[i+j]==s2[j];j++)
if(!s2[j+1]) return &s1[i];
return NULL;
}
http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm is another linear time algorithm. OTOH, the simple algorithms stay because in practise they are faster for most of the strings people actually search with.

factorial using recursion+pointers [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
i am learning C programming, i was trying to write a recursive function by using this prototype:
void fact(int *n);
The parameter of this function should be passed by reference. Thanks for your help.
I don't feel to be helpful in giving a complete solution -- this is just to show there is an answer:
void fact(int *n)
{
if (*n > 1)
{
int tmp = *n - 1;
fact(&tmp);
*n *= tmp;
}
}
I would never write a factorial function this way.

Resources