Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /*
- * File: main.c
- * Author: NgT
- *
- * Created on July 12, 2016, 11:12 PM
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- /*
- *
- */
- void changeString(char *s);
- void deleteChar(char *s);
- char* ltrim(char s[]) ;
- char* rtrim(char s[] );
- char* trim(char s[]);
- int main(int argc, char** argv) {
- char input[100] = "";
- printf("Enter a string: ");
- gets(input);
- printf("The old string: ");
- puts(input);
- trim(input);
- changeString(input);
- return (EXIT_SUCCESS);
- }
- void changeString(char *s) {
- char c[300];
- c[0] = '\0';
- int i;
- for (i = strlen(s) - 1; i >= 0; i--) {
- if (s[i] == ' ') {
- strcat(strcat(c, s + i + 1), " ");
- s[i] = '\0';
- }
- if (s[i] == '_') {
- strcat(strcat(c, s + i + 1), "_");
- s[i] = '\0'; //delete space end of string
- }
- }
- strcat(c, s);
- puts(c);
- }
- char* ltrim(char s[])
- {
- int i=0;
- while (s[i]=='_')i++;
- if (i > 0)
- strcpy(&s[0],&s[i]);
- return s;
- }
- char* rtrim(char s[])
- {
- int i=strlen(s)-1;
- while(s[i]=='_')i--;
- s[i+1]='\0';
- return s;
- }
- char* trim(char s[])
- {
- ltrim(s);
- rtrim(s);
- char *ptr = strstr(s,"_");
- while(ptr!=NULL)
- {
- strcpy(ptr,ptr+1);
- ptr = strstr(s,"__");
- }
- return s;
- }
Advertisement
Add Comment
Please, Sign In to add comment