-1
int length(char *);
char* reverse(char *);

int main()
{
    int a;
    char p;
    a=length("Computer");
    printf("%d", a);
    printf("\nReverse : %s", reverse("Computer"));
    getch();
    return 0;
}

int length(char *p)
{
    int i;
    for(i=0;*(p+i)!='\0'; i++);
    return(i);
}

char* reverse(char *p)
{
    int len, i;
    char temp;
    for(len=0; *(p+len)!='\0'; len++);
    for(i=0; i<len/2; i++)
    {
        temp=*(p+i);
        *(p+i)=*(p+len-1-i);
        *(p+len-1-i)=temp;
    }
    return(p);
}

I am trying to print the length of the string inputted without using strlen() function and also creating a user defined function using pointer and function to reverse a string without using strrev() function. After compilation the program doesn't throws an error but it just does not display anything. The length is being printed correctly but the reverse section is not being printed and I can't figure out why? Help me out here people.

D. Caruso
  • 163
  • 1
  • 3
  • 11
  • Will creating a char type pointer variable and storing the string in it and passing it through reverse(), fix the problem? – Mohsin Khan Aug 30 '20 at 10:26
  • @user3121023 Thanks it worked like a charm. I have another question though, you said memory would need to be allocated to the pointer but I guess it will eventually be allocated to the text[] as well. so whats the point of using an array instead of a pointer? – Mohsin Khan Aug 30 '20 at 10:41

1 Answers1

1

first of all, as user3121023 said, string constants (or literals) cannot be modified.

The problem was with indexes, pointer and at the end the piece of code that reverse the string. I adjust it in certain points and I'm gonna attach you here:

#include <stdio.h>
#include <stdlib.h>

int length(char *);
char * reverseWithDynamicMemory(char *, int);
char * reverseWithoutDynamicMemory(char *, int, char *);

int main() {
    char *pWord = "Computer";
    int wordLength = length(pWord);
    char reverseWordWithouDynamicMemory[wordLength];
    
    printf("Word Lenght: %d\n", wordLength);
    printf("\nReverse with Dynamic Memory: %s\n", reverseWithDynamicMemory(pWord, wordLength));
    printf("Reverse without Dynamic Memory: %s\n\n", reverseWithoutDynamicMemory(pWord, wordLength, reverseWordWithouDynamicMemory));
    
    return 0;
}

int length(char *pWord) {
    int i;
    for (i = 0; *(pWord + i) != '\0'; i++);
    return i;
}

char * reverseWithDynamicMemory(char *pWord, int length) {
    int i = 0, end = length - 1;
    char *reverseWord = (char *) malloc(sizeof(char) * length);
    if(!reverseWord) {
        printf("\nError allocating memory for reverseWord...\n");
        exit(EXIT_FAILURE);
    }

    while (i < end || end >= 0) {
        reverseWord[i] = pWord[end];
        end--;
        i++;
    }

    reverseWord[length] = '\0';
    return reverseWord;
}
char * reverseWithoutDynamicMemory(char *pWord, int length, char *reverseWord) {
    int i = 0, end = length - 1;

    while (i < end || end >= 0) {
        reverseWord[i] = pWord[end];
        end--;
        i++;
    }

    reverseWord[length] = '\0';
    return reverseWord;
}

Some useful tips:

  • There was an implicit declaration of function 'getch' which is invalid in C99
  • Unused variable 'p'
  • Use more descriptive names
  • I've created a variable with dynamic memory inside the function reverse. Otherwise address of stack memory associated with local variable 'reverseWord' is returned.

Best regards, Denny

D. Caruso
  • 163
  • 1
  • 3
  • 11
  • 1
    Hey Thanks Denny, actually I am just learning about Pointers and I have not used or learned about dynamic memory allocation yet.So can you tell me about a way which doesn't include Dynamic Memory Allocation, if you could I would be grateful. Or else I will learn about Dynamic Memory Allocation and then return to your code. BTW thanks alot for your help, those tips are precious. – Mohsin Khan Aug 30 '20 at 11:50
  • You're welcome Mohsin. Don't worry, tomorrow I will edit the answer with the solution without dynamic memory too. Pointers are quite difficult, but once mastered they will be really useful. – D. Caruso Aug 30 '20 at 18:12
  • @D.Caruso you might want to check the existing canonical Q/A and see if it could use an improvement instead... – Antti Haapala -- Слава Україні Aug 30 '20 at 19:16