We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
My solution in C may not be the optimal one, but I enjoyed coding it
#include<stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>voidprocesar_cadena(char*str){size_tlon=strlen(str);// Crear dos cadenas auxialiares para, imparchar*str_p=malloc(sizeof(char)*lon);char*str_i=malloc(sizeof(char)*lon);if(!str_p||!str_i)exit(1);// Iterar sobre cadena original para copiar en las posiciones pares e imparesinti=0,j=0;for(intk=0;k<strlen(str);k++){if(k%2==0)str_p[i++]=str[k];elsestr_i[j++]=str[k];}// Agregar 0 terminador cadenastr_p[i]=0;str_i[j]=0;// Formatear salida originalsprintf(str,"%s %s",str_p,str_i);// Liberar memoria;free(str_p);free(str_i);}intmain(){// Leer numero de strings y limpiar bufferintnum_cad=0;scanf("%d",&num_cad);intc;while((c=getchar())!='\n'){}char*cadenas[num_cad];// Crear tantas cadenas como n num_cadchar*linea=NULL;// Buffer para almacenar la entradasize_tlon=0;// Longitud cadena de entrada// Leer la entrada y almacenarlo en cada cadenafor(inti=0;i<num_cad;i++){// Leer la entrada con getline para manejar longitud de cadena desconocidaif(getline(&linea,&lon,stdin)==-1)exit(1);linea[strcspn(linea,"\n")]=0;// Limpiar salto de lineacadenas[i]=malloc(sizeof(char)*lon);// Asignar memoria a cadenasstrcpy(cadenas[i],linea);// Copiar el contenido de entrada a cadenas}// Procesar cadenasfor(inti=0;i<num_cad;i++){procesar_cadena(cadenas[i]);}// Printear cada cadenafor(inti=0;i<num_cad;i++){puts(cadenas[i]);}// Liberar memoriafor(inti=0;i<num_cad;i++){free(cadenas[i]);}return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 6: Let's Review
You are viewing a single comment's thread. Return to all comments →
My solution in C may not be the optimal one, but I enjoyed coding it