Error in inversion of a string

1

I'm trying to invert a string through a function but it is giving error, but I do not know where it is wrong.

Just below is my code.

#include<stdio.h>
#include<stdlib.h>
#include <string.h>

 char troca(char nome[100], char nome1[100]);

 int main(void)
{
   char nome[100];
   printf("Informe o nome para ser invertida :");
   scanf("%s", nome);
   printf("%s\n", troca(nome));
   system("pause");
   return 0;
}

char troca(char nome[100], char nome1[100])
  {
    int c = 0, i;
    for(i = strlen(nome) - 1; i >= 0; i--)
    {
      nome[c] = nome[i];
      c++;
    }
    nome1[c] = '
#include<stdio.h>
#include<stdlib.h>
#include <string.h>

 char troca(char nome[100], char nome1[100]);

 int main(void)
{
   char nome[100];
   printf("Informe o nome para ser invertida :");
   scanf("%s", nome);
   printf("%s\n", troca(nome));
   system("pause");
   return 0;
}

char troca(char nome[100], char nome1[100])
  {
    int c = 0, i;
    for(i = strlen(nome) - 1; i >= 0; i--)
    {
      nome[c] = nome[i];
      c++;
    }
    nome1[c] = '%pre%';
    return nome1[c];
  }
'; return nome1[c]; }
    
asked by anonymous 26.12.2017 / 17:30

1 answer

2

There are several errors there:

  • You are not passing the second argument, in fact the parameter is not necessary
  • Is not returning something useful by using
  • Not accepting spaces in data entry
  • Some errors would appear if some of these errors were corrected.

It gets better:

#include <stdio.h>
#include <string.h>

char *troca(char nome[100]) {
    for (int i = 0, j = strlen(nome) - 1; i < j; i++, j--) {
        char temp = nome[i];
        nome[i] = nome[j];
        nome[j] = temp;
    }
    return nome;
}

int main(void) {
   char nome[100];
   printf("Informe o nome para ser invertida :");
   scanf("%[^\n]s", nome);
   printf("%s\n", troca(nome));
}

See running on ideone . And in Coding Ground . Also put it in GitHub for future reference .

    
26.12.2017 / 17:43