Reverse a String With Out Using String Library Functions
|
Source Code
bool ReverseString(const char *src, char *dst, int len)
{
const char *p = src;
// count the length
int count = 0;
int i = 0, j = 0, k = 0;
while(1)
{
if(p == NULL || *p == '\0' ||
*p == '\t' || *p =='\n' || *p == '\r')
{
count = i;
break;
}
p = p + 1;
i = i + 1;
}
if(count > len)
{
return false; // Insufficient memory in the destination pointer
}
for(j = count - 1; j >= 0; j--)
{
dst[k++] = src[j];
}
dst[k] = '\0';
return true;
}
int main()
{
char src[] = "arif";
char dst[32];
ReverseString(src, dst, 32);
printf("%s\n%s\n", src, dst );
return 0;
}
Output
arif
fira
#include
উত্তরমুছুন#include
void reverse(char str[], int len)
{
if(str[len]=='\0')
{
return;
}
else
{
reverse(str, len+1);
}
printf("%c", str[len]);
}
int main()
{
char word[50];
int dig;
gets(word);
reverse(word, 0);
getch();
return 0;
}
@Md. Mahfuzur Rahman : Write Header file in details. It will be helpful for the beginners.
উত্তরমুছুনSir, I wrote Header files in this code, but after posting, those have become invisible.
উত্তরমুছুনIn this code, the header files are :
stdio.h
conio.h
#include
উত্তরমুছুনint main()
{
printf("Program to find ODD or Even Number\n");
while(1)
{
int i = 0;
printf("\nEnter a number(-1 for Exit): ");
scanf("%d",&i);
if( i == -1)
break;
if((i % 2) == 0)
{
printf("%d is a EVEN number.\n", i);
}
else
{
printf("%d is a ODD number.\n", i);
}
}
return 0;
}