-1
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    void str_prn(char **);
    int main(void)
    {
        char temp[80];
        char **str;
        
        int max;
        int i;
        printf("Number of Strings : ");
        scanf("%d", &max);
        str = (char **)malloc((max+1)*sizeof(char*));
        i=0;
        while(1)
        {
            printf("Input the string : ");
            scanf("%s",temp);
            str[i] = (char*)malloc(strlen(temp)+1);     
            strcpy(str[i],temp);
            
            i++;
            
            if(i ==max)
            {
                printf("Complete!!\n\n");
                break;
            }
        }
        str_prn(str);
        i=0;
        
        while(str[i]!=0)
        {
            free(str[i]);
            ++i;
        }
        free(str);
        system("PAUSE");
        return 0;
    }

    void str_prn(char **sp)
    {
        int i, len=0;
        char *reversedSp;
        
        len = strlen(*sp);
        
        while(*sp !=0)len++;
        {
            for(i=0;i<len;i++)
            {
                reversedSp = *sp
            }
        }
        
        return;
    }

I want to get the result of input strings reversed. I mean if I get "abcdeqwerty" the result should be result should be "edcbaytrewq"

First I used strrev func but my professor said not to use strrev. So I tried to deal with the matter by using for loop.

In the function str_prn, How can I revise it to work?

TomZanna
  • 7
  • 1
  • 5
  • [https://www.google.com/search?q=string+reversal+algorithm+in+c](https://www.google.com/search?q=string+reversal+algorithm+in+c) – Uroš Jan 10 '21 at 08:50
  • Put `[c] string reverse` in the search box of this site and you'll get *hundreds* of hits. And fyi, your professor is actually correct to avoid `strrev`, It is not part of the standard library. Also, `while(*sp !=0)len++;` is clearly not correct. the *body* of that loop is the single statement `len++;`. The braces and seemingly nested for-loop thereafter are not part of the `while` structure at all. Further, even after fixing that nothing advances `sp` or changes it in any way, and since its content (also unchanged) is the *only* condition to break that loop, it is a recipe for infinity. – WhozCraig Jan 10 '21 at 09:19
  • Search this site `"[c] reverse string"` and you will have hundreds of answers to choose from. – David C. Rankin Jan 10 '21 at 09:47
  • Thanks bros! I've solved! It would be a damn hard work unless you guys. – AmiablePrism30 Jan 10 '21 at 12:18

1 Answers1

0

You could try tou count the length of your string, and then start from the end...

UnDesSix
  • 68
  • 1
  • 1
  • 8