Advertisement
damn_ghad

Untitled

Oct 22nd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. void removesub(char* sentence, int start_pos, int len) {
  6.     for (int i = 0; i < len + 1; i++) {
  7.         unsigned int index = start_pos;
  8.         while (index < strlen(sentence) - 1) {
  9.             sentence[index] = sentence[index + 1];
  10.             index++;
  11.         }
  12.     }
  13.     sentence[strlen(sentence) - len - 1] = '\0';
  14. }
  15.  
  16. int word_count(const char sentence[])
  17. {
  18.     int counted = 0; // result
  19.  
  20.     // state:
  21.     const char* it = sentence;
  22.     int inword = 0;
  23.  
  24.     do switch (*it) {
  25.     case '\0':
  26.     case ' ': case '\t': case '\n': case '\r': // TODO others?
  27.         if (inword) { inword = 0; counted++; }
  28.         break;
  29.     default: inword = 1;
  30.     } while (*it++);
  31.  
  32.     return counted;
  33. }
  34.  
  35. int main(void) {
  36.     char sentence[256], duplicate[256];
  37.     gets_s(sentence, 256);
  38.     strcpy_s(duplicate, strlen(sentence) + 1, sentence);
  39.  
  40.     char* tmp = malloc(sizeof(char*));
  41.     char* word = strtok_s(duplicate, " ", &tmp);
  42.     int pos, curr_pos = 0;
  43.     scanf_s("%d", &pos);
  44.  
  45.     if (pos > word_count(sentence) - 1) {
  46.         printf("You are out of range! There are only %d words", word_count(sentence));
  47.         return 0;
  48.     }
  49.  
  50.     int index = strlen(word);
  51.     while (curr_pos != pos) {
  52.         word = strtok_s(NULL, " ", &tmp);
  53.         curr_pos++;
  54.         index += strlen(word) + 1;
  55.     }
  56.     index -= strlen(word);
  57.  
  58.     removesub(sentence, index, strlen(word));
  59.     puts(sentence);
  60.  
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement