Reverse a String

Reverse a String With Out Using String Library Functions


I have used while loop to find out the length of the string and for loop to reverse the string. The function name is ReverseString which does the work for you.

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

৪টি মন্তব্য:

  1. #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;
    }

    উত্তরমুছুন
  2. @Md. Mahfuzur Rahman : Write Header file in details. It will be helpful for the beginners.

    উত্তরমুছুন
  3. 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

    উত্তরমুছুন
  4. #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;
    }

    উত্তরমুছুন