Function to invert strings in C

Asked

Viewed 176 times

0

Inserts the orig string at the beginning of the dest string, returning dest.

char *strins(char *dest, char *orig)

My code is as follows::

char *strins(char *dest, char *orig)
{
    return strcat(*orig,*dest);
}

main()
{
    char s[] = "World";
    strins(s, "Hello");
    printf(s);
}

The function should work like this:

char s[100] = "Autonoma";
strins(s, "Universidade");
printf(s);  ->  UniversidadeAutonoma
  • 2

    Not string inversion. String inversion is transforming "roma" in "amor". What you want is to concatenate strings and return to a specific point

  • 2

    What’s the problem with your code? What’s going on?

  • 1

    char s[] = "World", what will be the size of s? Will there be room for you to add another text to it? The function strcat wait as parameter two pointers to char. The order of the arguments of strcat is destination and source, what you went through orig and dest in this order?

  • The code does not display the string even if it is s[20], for example.

  • The intention is for the argument to stay ahead

  • So which way out?

  • Returns only one value: "Process returned -1073741819 (0xC0000005)"

Show 2 more comments

1 answer

3


There are several problems in your code.

First, when you define a literal string with char s[] = "World"; you are allocating only the memory space required for that specific number of characters.

Since you want to join two strings, the size will increase, so you need more space. This is solved for example by setting a maximum size with char s[100] = "World";.

Also you can’t modify a literal string, so you can’t do the function the way you did and expect it to work with strins(s, "Hello"); since he won’t be able to modify the literal string "Hello".

Finally, the result of the function strcat is saved in the first parameter, so it would be saved in the orig instead of dest as desired. A strcpy will suffice, though will leave the source variable "dirty", what I don’t know if it’s a problem.

Follow the code with the corrected suggested problems:

#include <stdio.h>
#include <string.h>
char *strins(char *dest, char *orig)
{

    strcat(orig,dest);
    strcpy(dest,orig);
    return dest;
}

int main()
{
    char s[100] = "World";
    char outro[100]= "Hello";
    strins(s,outro);
    printf("%s\n",s);
    return 0;
}

That returns the result:

HelloWorld
  • If I’m not mistaken, you’re also changing the value of orig. Not that it is in the description of the problem does not change orig, but it would be appropriate not to change it. Thus, your code does not hold for the call string(s, "Universidade");, which is the basic example

  • I believe I’ve already made it clear in the answer what you quoted when I wrote: embora vá deixar a varíavel de origem "suja", o que não sei se é um problema.. Also, in the end, the printf made is exactly as proposed by the base example, and returns the request. I will edit the response to add the found result.

  • In my reading I went over that stretch.

  • Ok! No problem. It’s always good to try to make the answers as clear as possible.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.