Replacing characters in strings malloc

2019-09-03 02:59发布

问题:

Given a string and two characters, I want to find how many times the first character exists on the string, in what position it appears first and create a new string where the second character replaces the first every time it shows up, but I'm having problems with the last part. This is what I have so far:

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

main ()
{
    char string[10], string1;
    char c1, c2;
    int contador, i, l, n, contador2;

    printf ("Introduza uma string e dois caracteres.\n");
    scanf ("%s %c %c", string, &c1, &c2);

    l = strlen (string);
    contador = 0;

    for (n = 0; n < l; n++)
    {
        if (c1 == string[n])
        {
            contador = contador + 1;
        }
    }
    for (n = 0; n < l; n++)
    { 
        if (c1 == string[n])
        {
            contador2 = n + 1; /*Dá-se o valor de n + 1 à variável contador2 porque n começa em 0*/
            break;
        }
    }

    string1 = (char) malloc ((l +1)*sizeof(char));

    for (n = 0; n < l; n++)
    {
        if (c1 == string1[n])
        {
            n = c2;
        }
    }
    printf ("%d\n", contador);
    printf ("%d\n", contador2);
    printf ("%s", string1);
}

I'd appreciate any help you'd care to offer.

回答1:

Your declaration of string1 is wrong. It shouldn't be a char but a pointer to char, char*. Then you also wouldn't be tempted to cast the return value of malloc.

Some minor hints:

  • Don't declare several variables in one line, in particular if their types are different.
  • By definition sizeof(char) is 1 and malloc counts the size it allocates in the number of char.
  • Indent your code properly. Not only it makes it more readable for others, also you will appreciate it yourself if you revisit your code later.


回答2:

Declare string1 as char *

char *string1;  

and then allocate memory as

string1 = malloc ((l + 1)*sizeof(char));


回答3:

string1 is just a char - declare string1 as char * string1. Also your for loop for replacing characters was messed up. It should be:

 for (n = 0; n < l; n++)
 {
      if (c1 == string[n])// You should compare with string and not string1
      {
           string1[n] = c2;
      }
      else 
           string1[n]=string[n];
 }

Also there is no need to cast return of malloc in C - malloc returns void*. Your full corrected code:

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

int main ()
{
    char string[10],* string1;
    char c1, c2;
    int contador, i, l, n, contador2;

    printf ("Introduza uma string e dois caracteres.\n");
    scanf ("%s %c %c", string, &c1, &c2);

    l = strlen (string);
    contador = 0;

    for (n = 0; n < l; n++)
    {
        if (c1 == string[n])
        {
              contador = contador + 1;
        }
    }
    for (n = 0; n < l; n++)
    { 
        if (c1 == string[n])
        {
              contador2 = n + 1; /*Dá-se o valor de n + 1 à variável contador2 porque n começa em 0*/
             break;
        }
    }

  string1 =  malloc ((l +1)*sizeof(char));

  for (n = 0; n < l; n++)
  {
      if (c1 == string[n])
      {
          string1[n] = c2;
      }
    else 
         string1[n]=string[n];
  }
  printf ("%d\n", contador);
  printf ("%d\n", contador2);
  printf ("%s", string1);
}

I'd appreciate any help you'd care to offer.

Please start indenting your code. It took me more time to indent than solving the problem.