Advertisement
evgenko

Untitled

Mar 25th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <values.h>
  4. #include <string.h>
  5.  
  6. int get_min_length_of_world(char *string);
  7. void change_register(char *string);
  8. void add_space(char *string);
  9.  
  10. const int N = 200;
  11.  
  12. int main() {
  13.     char *string = malloc(N*sizeof(char));
  14.     printf("Enter the string: ");
  15.     fgets(string, N, stdin);
  16.     printf("%d\n", get_min_length_of_world(string));
  17.     change_register(string);
  18.     add_space(string);
  19.     printf("%s", string);
  20.     return 0;
  21. }
  22.  
  23. int get_min_length_of_world(char *string){
  24.     int min_length = INT_MAX;
  25.     char *firstletter = string;
  26.     char *lastletter = NULL;
  27.     for (char *letter = string; *letter!='\0'; letter++){
  28.         if (*letter == ' ' || *letter == '\n'){
  29.             lastletter = letter;
  30.             if (lastletter-firstletter < min_length)
  31.                 min_length = lastletter - firstletter;
  32.             firstletter = letter+1;
  33.         }
  34.     }
  35.     return min_length;
  36. }
  37. void add_space(char *string){
  38.     char *new_string = malloc(N*sizeof(char));
  39.     for (char *letter = string; *letter!='\0'; letter++){
  40.         char tmp[2] = {*letter, '\0'};
  41.         strcat(new_string, tmp);
  42.         if (*letter == ' '){
  43.             strcat(new_string, " ");
  44.             letter+=1;
  45.         }
  46.     }
  47.     strcpy(string, new_string);
  48.     free(new_string);
  49. }
  50. void change_register(char *string){
  51.     for (char *letter = string; *letter!=' '; letter++){
  52.        if (*letter>='a' && *letter<='z'){
  53.            *letter-=' ';
  54.        }else{
  55.            if (*letter>='A' && *letter<='Z'){
  56.                *letter+=' ';
  57.            }
  58.        }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement