I am unable to copy characters from one string to another

Asked

Viewed 160 times

0

I am making a program that until the user type +, the typed string will be copied to another, for example stack+, or the copied string would only stack, I tried to make the code below only that did not work.

#include <stdio.h>

int main(int argc, char** argv)
{
  char nome[100], num1[100];
  int cont,i;
  scanf("%s", nome);
  cont = 0;
  for(i = 0; i < nome[i] != '+'; i++)
  {
     num1[cont] = nome[i];
  }
    num1[cont] = '\0';
    printf("%d\n", cont);
  return 0;
}
  • C does not accept "Aggregate" conditions so your for should be "for(i = 0; i < name[i] && name[i] != '+'; i++)"

  • Thank you @Penachia, this detail went unnoticed

2 answers

4


As already commented on the condition of for is not correct and should be:

nome[i] != '\0' && nome[i] !='+'

Which means, as long as I don’t get to the end and as long as I don’t get to one '+'.

In addition to the cont is not being incremented within the for. Actually that cont nor is it necessary and so I suggest that you remove it and keep only the i which is more than enough to solve the problem.

Rewrite to:

#include <stdio.h>

int main(int argc, char** argv) {
    char nome[100], num1[100];
    int i; //sem cont
    scanf("%s", nome);

    for(i = 0; nome[i] != '\0' && nome[i] !='+'; i++) {
        num1[i] = nome[i]; //utilizando so o i
    }
    num1[i] = '\0'; //com i aqui tambem
    printf("%s\n", num1);
    return 0;
}

See the code running on Ideone

I changed the last printf by one that shows the string copied to make it easier to view the result.

I think the idea of showing the cont at the end was to show until which letter was copied. In this solution the final value of i has the same information if you want to show it.

  • Isac, I read here(google) that the \0 serves to delimit the end of the string. In this case there is a why to put num1[i] = '\0';? could you explain to me?

  • 1

    @Caiqueromero Yes. It is for the new string to also have the \0, otherwise copies all characters except the final delimiter (this is not copied as this is where the for ends). If you do not have this instruction you cause the string is not finished properly and potentially gives Segmentation fault further on.

  • Understood, thank you very much/

2

When you are walking the string just check that the character is equal to + and if it is you come out of the loop with a break;.

I saw that at the end you were displaying a counter that wasn’t even incremented, so it always displayed zero, I modified the printf to display the string until the first occurrence of +.

#include <stdio.h>

int main(int argc, char** argv)
{
  char nome[100], num1[100];
  int i;

  printf("Digite um texto ateh 100 caracteres:\n");
  scanf("%s", nome);

  for(i = 0; nome[i] != '\0'; i++){
    if(nome[i]!='+')
        num1[i] = nome[i];
    else
        break;

  }

  num1[i] = '\0';
  printf("Nova string: %s\n", num1);
  return 0;
}

inserir a descrição da imagem aqui

  • 2

    I think you have a problem with your for, the comparison i < nome[i] (or I didn’t get the logic right)

  • 3

    Now that I’ve seen your comment on the Isac. The num1[i] = '\0' is to put the finalizer in the resulting string too, which was not initialized (in the allocation the memory comes with undefined data, if you do not put the null at the end, when printing can receive something like Caiqueruf$&Pý¨$#*&hrifhr

  • 1

    in fact was without logic the first answer even (I was going to put the strlen() no for but I don’t know why I changed in the middle). I figured I’ll add my answer.

  • 2

    strlen is worse, you will already scan the string to copy, there is no reason to scan before just to know the size (until the strlen will do exactly that, increment a counter until you find the 0).

  • Yes, absolutely. When I said "add" in the comment above I meant the \0. Now I will try to understand how that first for worked (generated a silent error). Thanks for the explanation did not know this character issue \0

  • @Bacco found out why that for worked. When I compared a i with the char returned from nome[i] I was actually comparing a i with the ASCII value of char, example: printf("%d \n", 96<'a'); //Retorna 1. xD

  • 2

    Yes, it was a side effect where, by a "near chance" 0 was the only one below i for short strings - but if the string had more than 32 characters, especially more than 65 characters (which is 'A') probably the error would appear faster. In the 96 you mentioned, it would be "blatant". Anyway, the exercise to understand what happened is fruitful.

Show 2 more comments

Browser other questions tagged

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