Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reversing array in C

My array works fine if array is above 7 and if below 7 it will stuck at 7 and the remaining array will be filled with random characters


int main()
{
    char inputString[1001];

    for (int i = 0 ; i < sizeof(inputString) ; ++i) {
        scanf("%c", &inputString[i]);
    }
    int length=0;
    
    while(inputString[length] != '\0') length++;

    // Reverse String
    int x;
    printf("%d\n", length+1);
    for(x = length-1; x >= 0; x--) {
        printf("%c", inputString[x]);
    }

    return 0;
}

TBH, there are many errors in that code, but i don’t know how to fix it.

What can i do in this situation?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Start by initializing the array:

char inputString[1001] = {0};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading