Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

459
Views
Salto de línea inesperado en printf en c

Estoy aprendiendo cadenas en c, y estoy trabajando en mi tarea que me pide que escriba un programa para reemplazar parte de la cadena en ciertas circunstancias. Aquí está mi código fuente (deshacer):

 #include <stdio.h> #include <string.h> int main() { char str1[128], str2[128], str3[128]; for (int i = 0; i < 128; i++) //initialize str { str1[i] = 0; str2[i] = 0; str3[i] = 0; } printf("Input the first string:"); //inputs fgets(str1, 128, stdin); printf("Input the second string:"); fgets(str2, 128, stdin); printf("Input the third string:"); fgets(str3, 128, stdin); if (strncmp(str1, str2, strlen(str2) - 1) == 0) //if the first n charters match (n=length of str2) { printf("%s", str3); //print str3 int RemainingChar = 0; RemainingChar = strlen(str1) - strlen(str2); for (int i = 0; i < RemainingChar; i++) { printf("%c", str1[i + strlen(str2) - 1]); //print the remaining part } } return 0; }

Así es como funciona:

 Input the first string:asdfghjkl Input the second string:asd Input the third string:qwe qwe fghjkl

Hay un salto de línea inesperado. ¿Qué debo hacer para que salga así: qwefghjkl ?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

La función fgets también almacenará el carácter de nueva línea '\n' al final de la línea en la cadena. Si no desea que se imprima este carácter de nueva línea, debe eliminarlo antes de imprimir la cadena.

Consulte la siguiente pregunta para conocer varias formas de eliminar el carácter de nueva línea:

Eliminar el carácter de nueva línea final de la entrada fgets ()

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!