Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- void removesub(char* sentence, int start_pos, int len) {
- for (int i = 0; i < len + 1; i++) {
- unsigned int index = start_pos;
- while (index < strlen(sentence) - 1) {
- sentence[index] = sentence[index + 1];
- index++;
- }
- }
- sentence[strlen(sentence) - len - 1] = '\0';
- }
- int word_count(const char sentence[])
- {
- int counted = 0; // result
- // state:
- const char* it = sentence;
- int inword = 0;
- do switch (*it) {
- case '\0':
- case ' ': case '\t': case '\n': case '\r': // TODO others?
- if (inword) { inword = 0; counted++; }
- break;
- default: inword = 1;
- } while (*it++);
- return counted;
- }
- int main(void) {
- char sentence[256], duplicate[256];
- gets_s(sentence, 256);
- strcpy_s(duplicate, strlen(sentence) + 1, sentence);
- char* tmp = malloc(sizeof(char*));
- char* word = strtok_s(duplicate, " ", &tmp);
- int pos, curr_pos = 0;
- scanf_s("%d", &pos);
- if (pos > word_count(sentence) - 1) {
- printf("You are out of range! There are only %d words", word_count(sentence));
- return 0;
- }
- int index = strlen(word);
- while (curr_pos != pos) {
- word = strtok_s(NULL, " ", &tmp);
- curr_pos++;
- index += strlen(word) + 1;
- }
- index -= strlen(word);
- removesub(sentence, index, strlen(word));
- puts(sentence);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement