Problem with processing individual strings stored in an array of pointers to multiple strings in C

前端 未结 3 1374
無奈伤痛
無奈伤痛 2020-12-22 07:30

An array of pointers to strings is provided as the input. The task is to reverse each string stored in the input array of pointers. I\'ve made a function called reverseStrin

3条回答
  •  温柔的废话
    2020-12-22 07:50

    Hope this helps you! what i am doing here is that i am going to the address of the last character in the string then printing them all by decreasing the pointer by 1 unit (for character its 2 bytes(please check)).

    //program to reverse the strings in an array of pointers
    #include
    #include
    int main()
    {
        char *str[] = {
            "to err is human....",
            "But to really mess things up...",
            "One needs to know C!!"
        };
        int i=0;    //for different strings
        char *p;    //declaring a pointer whose value i will be setting to the last character in 
                    //the respective string
        while(i<3)  
        {
            p=str[i]+strlen(str[i])-1;
            while(*p!='\0')
            {
                printf("%c",*p);
                p--;
            }
            printf("\n");       
            i++;
        }
    }
    

提交回复
热议问题