簡體   English   中英

如何在 c 中使用點分隔符反轉字符串中的單詞 維護 output 中的點

[英]how to reverse the words in string with dot delimiter in c maintaining the dot in output

我想在 c 中用點反轉字符串的單詞。

我嘗試在 c 中使用 for 循環。

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

int main()
{
    char str[100];
    scanf("%s",str);
    printf("%s",str);
    int length = strlen(str); 

    // Traverse string from end 
    int i; 
    for (i = length - 1; i >= 0; i--) { 
        if (str[i] == '.') { 

            // putting the NULL character at the  
            // position of space characters for 
            // next iteration.          
            str[i] = '.'; 

            // Start from next charatcer      
            printf("%s ", &(str[i]) + 1); 
        } 
    } 

    // printing the last word 
    printf("%s", str); 

    return 0;
} 

示例輸入

i.love.you

示例 output

you.love.i

這就是我的做法(省略錯誤檢查):

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

int main(int argc, char *argv[])
{
    char str[100];
    char *c;

    scanf("%s", str);

    while ((c = strrchr(str, '.'))) {
        *c = 0;
        printf("%s.", c + 1);  
    }
    printf("%s\n", str);

    return 0;
}

Output:

$ ./a.out
this.is.a.test.for.stackoverflow
stackoverflow.for.test.a.is.this

嗯,有兩個明顯的錯誤:

  1. 您實際上從未終止(不是 NULL)要傳遞給printf的字符串。 我懷疑你從某個地方或某人那里復制了那部分。
// putting the NULL character at the  
// position of space characters for 
// next iteration.          
str[i] = '.'; 
  1. 實際上打印空格而不是任何分隔符:
printf("%s ", &(str[i]) + 1);
/*        ^ */

我會做什么:

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

struct {
  char input[100];
  char result[100];
}testcases[] = {
  {.input = "i.love.you",
  .result = "you.love.i"},
  {.input = ".hi.you",
  .result = "you.hi."},
  {.input = "there.is.no spoon",
  .result = "no spoon.is.there"},
  {.input = "..",
  .result = ".."},
  {.input = "oh.no.",
  .result = ".no.oh"},
  {.input = "",
  .result = ""}
};

char *function(char * s) {
  size_t max_length = strlen(s) + 1;
  /* Choosing to allocate and return a new string,
     but you can do whatever you like: */
  char * scratch = malloc(max_length);
  /* Keeping an offset to where we concatenate substrings in the output: */
  size_t offset = 0;

  for (char * delimiter = NULL; (delimiter = strrchr(s, '.')) != NULL;) {
    /* NUL-terminate where delimiter was found, without modifying the input argument,
       we'll need it later: */
    *delimiter = 0;
    /* Copy the substring after the delimiter: */
    int nwritten = snprintf(scratch + offset, max_length - offset, "%s.", delimiter + 1);
    /* Adjust offset: */
    offset += nwritten;
  }
  /* Now copy what's left over (if anything) at the beginning of the input string: */
  snprintf(scratch + offset, max_length - offset, "%s", s);
  return scratch;
}

int main(void) {
  for (size_t idx = 0; idx < sizeof testcases / sizeof *testcases; ++idx) {
    char * reversed = function(testcases[idx].input);
    assert(!strcmp(reversed, testcases[idx].result));
    printf("Testcase #%lu passed\n", idx);
    free (reversed);
  }
  return EXIT_SUCCESS;
}

希望你有時間在你的作業截止日期之前了解它是如何工作的。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM