2

I was trying to inverse a string in C. And I met some trouble defining the destination string.

And here are my thoughts: (I am a beginner, so my thoughts might seem laughable in your eyes...)

  1. I define a string data type with unknown length and assign a string to it. (In my case it's str1)

  2. I get the string length of string 1 and increment this value and assign to an integer. (l in my case. The +1 is to compensate the '\0' at the end of the string)

  3. Use this l integer to define a new string which is the destination string. (str2 in my case.)

  4. Use a for loop to copy from the end of str1 to the start of str2 and add a '\0' to the end of str2 to make it a string.

However, when I try to do this in visual studio 2013, it does not let me compile.

I have attached the error message and the code. (Line 15 in the error message is where I define str2 with integer l)

I tried to just put in a number (for example: 100) at the place where integer l was, and the code works.

Can you let me know what is wrong with defining a string with an integer variable and how to do it if there is a way in C, instead of just defining a long enough sting?

Thank you!

#include<stdio.h>
#include<string.h>
int main()
{
    /* Reversing a string! */

    int l; /* Length of str1 +1 (for the NULL character) */

    char str1[] = "This is an unknown length string!";
    l = strlen(str1) + 1;

    char str2[l]; /* The destination string */    
    int i, j;


    for (i = strlen(str1)-1,j=0; i < strlen(str1) && i >= 0; i--,j++)
        str2[j] = str1[i];
    str2[j] = '\0';

    /* Display str1 and str2 */

    printf("The content of str1: %s\n", str1);
    printf("The content of str2: %s\n", str2);
    return 0;
}

enter image description here

2 Answers 2

1

Visual studio doesn't support mix type declaration and variable length arrays. You need to declare all variables in C89 style, i.e at the beginning of your code in main. And dynamically allocate space for str2.

size_t l; /* Length of str1 +1 (for the NULL character) */
int i, j;
char str1[] = "This is an unknown length string!";
l = strlen(str1) + 1;

char *str2;   
str2 = malloc(l);
Sign up to request clarification or add additional context in comments.

2 Comments

Use size_t rather than int for array lengths is more portable.
Don't forget to free(str2); with this technique. Granted, in a program that's about to exit, it isn't crucial, but it is best to get into good habits from the start, and freeing allocated memory when it is no longer needed is definitely a good habit.
1

An alternative that doesn't need dynamic memory allocation. It works in this scenario, but it isn't as general a solution (you couldn't sensibly use it to reverse an arbitrary number of strings of different lengths, for example, whereas you can use malloc() and free() to handle that). Your original code uses a VLA — variable length array — which is standard in C99 and optionally supported in C11.

#include <stdio.h>

int main(void)
{
    /* Reversing a string! */
    char str1[] = "This is an unknown length string!";
    char str2[sizeof(str1)];
    int len = sizeof(str1);
    int i, j;

    for (i = len - 2, j = 0; i >= 0; i--, j++)
        str2[j] = str1[i];
    str2[j] = '\0';

    /* Display str1 and str2 */
    printf("The content of str1: %s\n", str1);
    printf("The content of str2: %s\n", str2);
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.