Удалите первое слово из массива char и распечатайте его, используя указатели на C - PullRequest
0 голосов
/ 02 мая 2018

Итак, я пытаюсь создать небольшую программу, в которой у меня есть строка со словом, и мне нужно удалить из нее n-е слово, а затем распечатать его с помощью указателей.

Я сделал часть, где я могу удалить n-е слово, но я не могу понять, как напечатать его с помощью указателей.

РЕДАКТИРОВАТЬ (Извините, я забыл добавить код):

#include <stdio.h>
#include <string.h>

void removeAll(char * str, char * toRemove);

int main(){
    char *words[100];
    char removeword[100];
    printf("Enter your sentence: ");
    gets(words);
    printf("Enter word to delete: ");
    gets(removeword);
    removeAll(words, removeword);
    printf("Word after deleting: %s", words);

}

void removeWord(char * str, char * toRemove)
{
    int i, j, stringLen, toRemoveLen;
    int found;

    stringLen   = strlen(str);
    toRemoveLen = strlen(toRemove);


    for(i=0; i <= stringLen - toRemoveLen; i++)
    {
        found = 1;
        for(j=0; j<toRemoveLen; j++)
        {
            if(str[i + j] != toRemove[j])
            {
                found = 0;
                break;
            }
        }

        if(str[i + j] != ' ' && str[i + j] != '\t' && str[i + j] != '\n' && str[i + j] != '\0')
        {
            found = 0;
        }


        if(found == 1)
        {
            for(j=i; j<=stringLen - toRemoveLen; j++)
            {
                str[j] = str[j + toRemoveLen];
            }

            stringLen = stringLen - toRemoveLen;

            i--;
        }
    }
}

РЕДАКТИРОВАТЬ 2:

char *pWord = words;
for (int i=0; pWord[i]; ++i) {
    const char *ch = pWord[i];
    while(*ch) {
        putchar(*ch++);
        putchar('\n');
    }
    putchar('\n');
}

1 Ответ

0 голосов
/ 02 мая 2018

В вашем коде есть некоторые ошибки времени компиляции. То есть вы объявили и использовали функцию void removeAll(char * str, char * toRemove); вместо void removeWord(char * str, char * toRemove).

Ниже исправлен рабочий код. Смотрите это работает здесь :

#include <stdio.h>
#include <string.h>

void removeWord(char * str, char * toRemove);

int main(){
    char words[100];
    char origional[100];
    char removeword[100];
    printf("Enter your sentence: ");
    gets(words);
    printf("Enter word to delete: ");
    gets(removeword);
    strcpy(origional, words);
    removeWord(words, removeword);
    printf("\nWord before deleting: %s", origional);
    printf("\nWord after deleting: %s", words);

}

void removeWord(char * str, char * toRemove)
{
    int i, j, stringLen, toRemoveLen;
    int found;

    stringLen   = strlen(str);
    toRemoveLen = strlen(toRemove);


    for(i=0; i <= stringLen - toRemoveLen; i++)
    {
        found = 1;
        for(j=0; j<toRemoveLen; j++)
        {
            if(str[i + j] != toRemove[j])
            {
                found = 0;
                break;
            }
        }

        if(str[i + j] != ' ' && str[i + j] != '\t' && str[i + j] != '\n' && str[i + j] != '\0')
        {
            found = 0;
        }


        if(found == 1)
        {
            for(j=i; j<=stringLen - toRemoveLen; j++)
            {
                str[j] = str[j + toRemoveLen];
            }

            stringLen = stringLen - toRemoveLen;

            i--;
        }
    }
}

Выход:

Enter your sentence: Hello there how are you?
Enter word to delete: how
Word before deleting: Hello there how are you?
Word after deleting: Hello there  are you?
...