fork download
  1. #include <stdio.h>
  2. char* convert(char* s, int numRows){
  3. if(numRows == 1)
  4. return s;
  5. int length_s = 0, diff = 0, index = 0, i, j=0;
  6. int toggle = 0;
  7. while(s[length_s] != '\0')
  8. length_s ++;
  9. printf("length_s %d \n",length_s);
  10. char* converted_string = (char*)malloc(length_s * sizeof(char*));
  11. diff = (numRows * 2) - 2;
  12. for(i = 1; i <= numRows; i++){
  13. toggle = 0;
  14. index = i;
  15. while(index <= length_s){
  16. if(i == 1 || i == numRows){
  17. converted_string [j] = s[index - 1];
  18. index = index + diff;
  19. } else{
  20. converted_string[j] = s[index - 1];
  21. if(toggle == 0)
  22. index = index + ((numRows - i)*2);
  23. else
  24. index = index + (diff - ((numRows - i)*2));
  25. if(toggle == 0)
  26. toggle = 1;
  27. else
  28. toggle = 0;
  29. }
  30. j++;
  31. }
  32. }
  33. //printf("converted_string %s\n",converted_string);
  34. int final_length_s = 0;
  35. while(s[final_length_s] != '\0')
  36. final_length_s ++;
  37. printf("length_s %d \n",final_length_s);
  38. return converted_string;
  39. }
  40. int main()
  41. {
  42. char* input_string;
  43. int numRows;
  44. input_string = "Apalindromeisaword,phrase,number,orothersequenceofunitsthatcanbereadthesamewayineitherdirection,withgeneralallowancesforadjustmentstopunctuationandworddividers.";
  45. numRows = 2;
  46. printf("%s \n",convert(input_string,numRows));
  47. return 0;
  48. }
Success #stdin #stdout 0s 2184KB
stdin
Standard input is empty
stdout
length_s 160 
length_s 160 
Aaidoeswr,haenme,rtesqecouishtabrateaeaietedrcinwtgnrlloacsoajsmnsoucutoadodiiesplnrmiaodprs,ubroohreunefnttacneedhsmwynihrieto,iheeaalwnefrdutettpntainnwrdvdr.