Nguythang

reverseString

Jul 14th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.50 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. /*
  8.  * File:   main.c
  9.  * Author: NgT
  10.  *
  11.  * Created on July 12, 2016, 11:12 PM
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17.  
  18. /*
  19.  *
  20.  */
  21. void changeString(char *s);
  22. void deleteChar(char *s);
  23. char* ltrim(char s[])  ;
  24. char* rtrim(char s[] );
  25. char* trim(char s[]);
  26. int main(int argc, char** argv) {
  27.     char input[100] = "";
  28.     printf("Enter a string: ");
  29.     gets(input);
  30.     printf("The old string: ");
  31.     puts(input);
  32.     trim(input);
  33.     changeString(input);
  34.     return (EXIT_SUCCESS);
  35. }
  36.  
  37. void changeString(char *s) {
  38.     char c[300];
  39.     c[0] = '\0';
  40.     int i;
  41.     for (i = strlen(s) - 1; i >= 0; i--) {
  42.         if (s[i] == ' ') {
  43.             strcat(strcat(c, s + i + 1), " ");
  44.             s[i] = '\0';
  45.         }
  46.         if (s[i] == '_') {
  47.             strcat(strcat(c, s + i + 1), "_");
  48.             s[i] = '\0'; //delete space end of string
  49.         }
  50.     }
  51.     strcat(c, s);
  52.     puts(c);
  53. }
  54. char* ltrim(char s[])  
  55. {
  56.     int i=0;
  57.     while (s[i]=='_')i++;
  58.     if (i > 0)
  59.     strcpy(&s[0],&s[i]);
  60.     return s;
  61. }
  62. char* rtrim(char s[])
  63. {
  64.     int i=strlen(s)-1;
  65.     while(s[i]=='_')i--;
  66.     s[i+1]='\0';
  67.     return s;
  68. }
  69. char* trim(char s[])
  70. {
  71.     ltrim(s);
  72.     rtrim(s);
  73.     char *ptr = strstr(s,"_");
  74.     while(ptr!=NULL)
  75.     {
  76.         strcpy(ptr,ptr+1);
  77.         ptr = strstr(s,"__");
  78.     }
  79.     return s;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment