Advertisement
Guest User

Untitled

a guest
Sep 25th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. int main()
  7. {
  8. char* input = "newfags can't strcat";
  9. char* copied;
  10. int copyFlag = 0;
  11. int i = 0;
  12. int k = 0;
  13. int len = 0;
  14.  
  15. // Allocate a buffer. For this, we will allocate it the same dimensions as input has.
  16. len = strlen(input);
  17. copied = (char *)malloc(len);
  18.  
  19. // Zero out the memory, for the same length of bytes obtained above.
  20. // \0 is a null terimator. We use this to inform functions like printf that they have
  21. // reached the end of the data and any data beyond it is garbage or unused.
  22.  
  23. memset(copied, '\0', len);
  24.  
  25. // Loop through each character
  26.  
  27. for (i = 0; i < len; i++)
  28. {
  29. if (copyFlag == 0) // Have not found a blank space yet
  30. {
  31. if (input[i] == ' ') // Is this a blank space?
  32. {
  33. copyFlag = 1; // Begin Copying
  34. }
  35. }
  36. else if (copyFlag == 1) // Found a blank space, so copy
  37. {
  38. if (input[i] != ' ')
  39. {
  40. copied[k] = input[i]; // Copy current character to index k
  41. k++; // Move to next index.
  42. }
  43. else
  44. {
  45. break; // No more copying is needed
  46. }
  47. }
  48. }
  49.  
  50. printf(copied); // Display the message
  51. printf("\n"); // new line
  52. free(copied); // Free the memory we allocated
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement