I can not copy the characters from one string to another [closed]

0

I am doing a program that until the user type +, the typed string will be copied to another, for example stack +, 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] = '
#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] = '%pre%';
    printf("%d\n", cont);
  return 0;
}
'; printf("%d\n", cont); return 0; }
    
asked by anonymous 03.04.2018 / 19:43

2 answers

4

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

nome[i] != '
#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] != '
nome[i] != '
#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] != '%pre%' && nome[i] !='+'; i++) {
        num1[i] = nome[i]; //utilizando so o i
    }
    num1[i] = '%pre%'; //com i aqui tambem
    printf("%s\n", num1);
    return 0;
}
' && nome[i] !='+'
' && nome[i] !='+'; i++) { num1[i] = nome[i]; //utilizando so o i } num1[i] = '%pre%'; //com i aqui tambem printf("%s\n", num1); return 0; }
' && nome[i] !='+'

What it means: until you reach the end and until you catch a '+' .

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

Rewrite to:

%pre%

See the code running on Ideone

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

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

    
03.04.2018 / 20:03
2

When you are traversing string , just check if the character is equal to + and if it is you leave the loop with break; .

I saw that in the end you were displaying a counter that was not 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] != '
#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] != '%pre%'; i++){
    if(nome[i]!='+')
        num1[i] = nome[i];
    else
        break;

  }

  num1[i] = '%pre%';
  printf("Nova string: %s\n", num1);
  return 0;
}
'; i++){ if(nome[i]!='+') num1[i] = nome[i]; else break; } num1[i] = '%pre%'; printf("Nova string: %s\n", num1); return 0; }

    
03.04.2018 / 20:12