![](/img/trans.png)
[英]Create a new string that will consist of common letters from other two strings
[英]add additional letters in a string if there are two same letters beside each other
如果彼此旁邊有兩個相等的字母,我會嘗試添加一個額外的字母。 這就是我的想法,但它沒有在兩個字母之間放入 x; 取而代之的是,它復制了雙字母之一,例如,現在我擁有 MMM 而不是 MXM。
for (index_X = 0; new_text[index_X] != '\0'; index_X++)
{
if (new_text[index_X] == new_text[index_X - 1])
{
double_falg = 1;
}
text[index_X] = new_text[index_X];
}
if (double_falg == 1)
{
for (counter_X = 0; text[counter_X] != '\0'; counter_X++)
{
transfer_X = counter_X;
if (text[transfer_X - 1] == text[transfer_X])
{
text_X[transfer_X] = 'X';
cnt_double++;
printf("%c\n", text[transfer_X]);
}
text_X[transfer_X] = text[transfer_X - cnt_double];
}
printf("%s\n", text_X);
}
如果您嘗試在text_X
創建修改后的數組,從new_text
復制數據並將X
放在相鄰的重復字母之間(忽略輸入包含XX
的可能性),那么您只需要:
char new_text[] = "data with appalling repeats";
char text_X[SOME_SIZE];
int out_pos = 0;
for (int i = 0; new_text[i] != '\0'; i++)
{
text_X[out_pos++] = new_text[i];
if (new_text[i] == new_text[i+1])
text_X[out_pos++] = 'X';
}
text_X[out_pos] = '\0';
printf("Input: [%s]\n", new_text);
printf("Output: [%s]\n", text_X);
當包含在基本的main()
函數中(和enum { SOME_SIZE = 64 };
)時,會產生:
Input: [data with appalling repeats]
Output: [data with apXpalXling repeats]
要處理輸入中重復的 X,您可以使用:
text_X[out_pos++] = (new_text[i] == 'X') ? 'Q' : 'X';
似乎您的方法比需要的更復雜 - 涉及太多循環和太多數組。 一個循環和兩個數組應該可以。
下面的代碼使用idx
迭代原始字符串以跟蹤位置,並使用變量char_added
來計算已添加到新數組中的額外字符數。
#include <stdio.h>
#define MAX_LEN 20
int main(void) {
char org_arr[MAX_LEN] = "aabbcc";
char new_arr[MAX_LEN] = {0};
int char_added = 0;
int idx = 1;
new_arr[0] = org_arr[0];
if (new_arr[0])
{
while(org_arr[idx])
{
if (org_arr[idx] == org_arr[idx-1])
{
new_arr[idx + char_added] = '*';
++char_added;
}
new_arr[idx + char_added] = org_arr[idx];
++idx;
}
}
puts(new_arr);
return 0;
}
輸出:
a*ab*bc*c
注意:代碼未經過全面測試。 它也缺乏越界檢查。
在您的最小、完整和可驗證示例 (MCVE) (MCVE) 中還有很多需要改進的地方。 但是,話雖如此,您需要做的是相當簡單的。 舉個簡單的例子:
"ssi"
根據您的說法,您需要在相鄰的's'
字符之間添加一個字符。 (您可以使用任何您喜歡的分隔符,但如果您的輸入是正常的 ASCII 字符,那么您可以將當前字符設置為下一個 ASCII 字符(如果當前是最后一個 ASCII 字符'~'
則減去一個))參見ASCII表和說明。
例如,您可以使用memmove()
將所有以當前字符開頭的字符上移一位,然后將當前字符設置為替換字符。 您還需要跟蹤當前長度,以免寫入超出數組范圍。
一個簡單的函數可能是:
#include <stdio.h>
#include <string.h>
#define MAXC 1024
char *betweenduplicates (char *s)
{
size_t len = strlen(s); /* get length to validate room */
if (!len) /* if empty string, nothing to do */
return s;
for (int i = 1; s[i] && len + 1 < MAXC; i++) /* loop until end, or out of room */
if (s[i-1] == s[i]) { /* adjacent chars equal? */
memmove (s + i + 1, s + i, len - i + 1); /* move current+ up by one */
if (s[i-1] != '~') /* not last ASCII char */
s[i] = s[i-1] + 1; /* set to next ASCII char */
else
s[i] = s[i-1] - 1; /* set to previous ASCII char */
len += 1; /* add one to len */
}
return s; /* convenience return so it can be used immediately if needed */
}
將要檢查的字符串作為第一個參數的簡短示例程序可以是:
int main (int argc, char **argv) {
char str[MAXC];
if (argc > 1) /* if argument given */
strcpy (str, argv[1]); /* copy to str */
else
strcpy (str, "mississippi"); /* otherwise use default */
puts (str); /* output original */
puts (betweenduplicates (str)); /* output result */
}
示例使用/輸出
$ ./bin/betweenduplicated
mississippi
mistsistsipqpi
或者當沒有什么可以替換時:
$ ./bin/betweenduplicated dog
dog
dog
或檢查極端情況:
$ ./bin/betweenduplicated "two spaces and alligators ~~"
two spaces and alligators ~~
two ! spaces ! and ! almligators ! ~}~
有多種方法可以接近它。 如果您還有其他問題,請告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.