【问题标题】:C program function to replace one word with anotherC程序函数用另一个词替换一个词
【发布时间】:2018-08-09 13:41:09
【问题描述】:

我很困惑创建一个函数,该函数将打印一个字符串并要求用户输入两个数字,然后函数将这些单词的数量相互替换。 我已将下面的图像添加为示例。 enter image description here

这是我的作业,我创建了其他 3 个函数,但并没有真正得到这个。 有人可以帮助我如何将单词转换为数字,然后将这些单词的数量相互替换。

这是我的程序,它可以将字符串分解成单词,但是我如何替换单词的位置。

   #include <stdio.h>
#include <string.h>
int main()
{
    char str1[100];
    char newString[10][10]; 
    int i,j,ctr;
       printf("\n\n Split string by space into words :\n");
       printf("---------------------------------------\n");    

    printf(" Input  a string : ");
    fgets(str1, sizeof str1, stdin);    

    j=0; ctr=0;
    for(i=0;i<=(strlen(str1));i++)
    {
        // if space or NULL found, assign NULL into newString[ctr]
        if(str1[i]==' '||str1[i]=='\0')
        {
            newString[ctr][j]='\0';
            ctr++;  //for next word
            j=0;    //for next word, init index to 0
        }
        else
        {
            newString[ctr][j]=str1[i];
            j++;
        }
    }
    printf("\n Strings or words after split by space are :\n");
    for(i=0;i < ctr;i++)
        printf(" %s\n",newString[i]);
    return 0;
}

【问题讨论】:

  • 建议将句子逐字逐句读成一个单词数组,从那里开始,剩下的应该很容易
  • 那个例子不是很好。如果从字面上理解,逗号将很难处理。
  • 我现在已经添加了我当前的代码。
  • @RetiredNinja 会说这个例子真的很好,因为它展示了如何处理逗号。
  • 在哪里提示输入要交换的单词的索引?

标签: c arrays string function


【解决方案1】:

在我看来,到目前为止您做得很好(您的代码无法处理逗号,但您可以稍后添加)。因此,我们假设您的 newString 实际上包含单个单词。

因此,下一步是从 newString 中的单个单词构造一个新字符串 str2。当你这样做时,你可以简单地交换两个感兴趣的词。要构建新字符串,strcat 函数可能会有所帮助。

下面的代码并不完全正确,但它可能会给你一些继续做功课的想法:

int lowest_index_to_swap = some_number
int highest_index_to_swap = some_higher_number

char str2[100] = "";

for (i=0; i<number_of_words_found; ++i)
{
     if (i == lowest_index_to_swap)
         strcat(str2, newString[highest_index_to_swap];
     else if (i == highest_index_to_swap)
         strcat(str2, newString[lowest_index_to_swap];
     else
         strcat(str2, newString[i];
     strcat(str2, " ";
}

【讨论】:

  • strcat 可能会起作用,但是使用得越多,它的效率就会越低。教科书“Shlemiel The Painter”算法。 joelonsoftware.com/2001/12/11/back-to-basics
  • @ChristianGibbons strcat 的性能很少对从标准输入读取用户输入的程序感兴趣
【解决方案2】:

这是我根据您在图像中的输入在本地服务器上尝试的代码 sn-p。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 100
#define WORDLEN 20

int main() {
    int place_1, place_2, count = 0, i, k =0;
    char str[] = "Hi, welcome to C programming";
    char **words = (char **)malloc(sizeof(char *)*SIZE);
    if(!words){
        printf("malloc of words failed!\n");
        exit(1);
    }
    char *temp = (char *)malloc(sizeof(char)*WORDLEN);
    if(!temp){
        printf("malloc of temp failed!\n");
        exit(1);
    }
    for(i = 0; str[i] != '\0'; count++){
        words[count] = (char *)malloc(sizeof(char)*WORDLEN);
        if(!words[count]){
            printf("malloc of words[%d] failed!\n", count);
            exit(1);
        }
        sscanf(str+i, "%s", words[count]);
        i += 1+strlen(words[count]);
        printf("%d %s %d\n", count, words[count], i);
    }
    printf("Enter the word places to replace: ");
    if(scanf("%d %d", &place_1, &place_2) < 2){
        printf("scanf failed!\n");
        exit(1);
    }
    temp = words[place_1 - 1];
    words[place_1 - 1] = words[place_2 - 1];
    words[place_2 - 1] = temp;
    for(i = 0; i < count; i++){
        sprintf(str+k, "%s ", words[i]);
        k += 1+strlen(words[i]);
    }
    printf("str: %s\n", str);
    free(temp);
    for(i = 0; i < count; i++){
        free(words[i]);
    }
    free(words);
}

希望对你有帮助。

【讨论】:

  • 我的错误。现在我添加了对 scanf 和 malloc 的检查。
猜你喜欢
  • 2012-02-26
  • 2019-09-24
  • 1970-01-01
  • 2021-07-22
  • 1970-01-01
  • 1970-01-01
  • 2015-03-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多