[英]Adding common characters in two strings into a new string
我在 C 中寫了一個 function ,如下所示:
char *common(char *a, char *b){
char *result = "";
for (int i = 0; i < strlen(a); i++){
for (int j = 0; j < strlen(b); j++){
if (a[i] == b[j]){
printf("%c", a[i]);
}
}
}
return result;
}
它應該比較兩個給定的字符串,並用這些字符串的公共字符返回一個新字符串。 例如,strings: hello world和thank you會返回一個字符串ho 。
現在它只打印字符並且結果為空,因為我在獲取結果字符串中的字符時遇到了問題。 在這種情況下我該怎么做?
另外,結果字符串不應包含重復字符,但每個字母僅包含一次。 我也不知道該怎么做,但這不是主要問題。
我希望它會對你有所幫助,它是一個有效的代碼。 我只是對您的程序進行了非常小的更改並添加了所需的內容。 很快我會優化這個。 但首先 go 通過下面的代碼來理解這個概念。
#include<stdio.h>
int isCharExist(char *str, char c){
int i=0;
while(str[i] != NULL){
if(str[i] == c){
return 1;
}
i++;
}
return 0;
}
char *common(char *a, char *b){
char *result = "";
int index = 0,i,j;
for (i = 0; i < strlen(a); i++){
for ( j = 0; j < strlen(b); j++){
if (a[i] == b[j]){
if(!isCharExist(result, a[i])){
result[index++] = a[i];
result[index] = '\0';//last character of string is always NULL
}
}
}
}
return result;
}
void main(){
char *result = "",*str1,*str2;
//better to assign space before assignment
str1 = "hello world";
str2 = "thank you";
//clrscr();
result = common(str1,str2);
printf("%s",result);
}
下面的代碼比上面的代碼要好,並且有一些小的改動,為了您更好地理解,我不會刪除這個代碼。
#include<stdio.h>
int isCharExist(char *result, char c);
char *common(char *a, char *b);
int isCharExist(char *result, char c){
//this function help you to find that current character
//is in your result or not
int i=0;
while(result[i] != NULL){
if(result[i] == c){
//if it find character in result string it return 1
return 1;
}
i++;
}
return 0;//if not find then it return 0
}
char *common(char *a, char *b){
char *result = "";
int index = 0,i,j;
//it is good to keep length in seprate variable before loop
//in loop it calculate length each time
int len1 = strlen(a);
int len2 = strlen(b);
for (i = 0; i < len1; i++){
for ( j = 0; j < len2; j++){
//check equality but not for blank
if (a[i] == b[j] && a[i] != ' '){
if(!isCharExist(result, a[i]))
{
result[index++] = a[i];
result[index] = '\0';//last character of string is always NULL
}
}
}
}
return result;
}
void main(){
char *result = "",*str1,*str2;
//better to assign space before assignment
str1 = "hello world";
str2 = "thank you";
//clrscr();
result = common(str1,str2);
printf("%s",result);
}
以上只是你的function的一些改動,所以只需要替換你的function不完整的代碼。
char *common(char *a, char *b){
char *result = "";
int index = 0,i,j,k, isDuplicate=0;
//it is good to keep length in seprate variable before loop
//in loop it calculate length each time
int len1 = strlen(a);
int len2 = strlen(b);
for (i = 0; i < len1; i++){
for ( j = 0; j < len2; j++){
//check equality but not for blank
if (a[i] == b[j] && a[i] != ' '){
k = 0;
while(result[k] != '\0'){
if(a[i] == result[k]){
isDuplicate = 1;
break;
}
k++;
}
if(!isDuplicate)
{
result[index++] = a[i];
result[index] = '\0';//last character of string is always NULL
}
isDuplicate = 0;
}
}
}
return result;
}
首先,您需要有一個char
數組來存儲常用字符。 然后,您需要按照它們出現的順序存儲它們,但還要證明該字符是否已分配給數組。 在循環完全結束后,您必須在以下元素中分配\0
null 字符以指示字符串的結尾。 此外,您必須向額外的參數傳遞一個指向調用者中的char
數組的指針,其中存儲了具有常見字符的字符串,另一個參數的大小是該數組的大小,因為它的大小無法在 function 中確定:
char *common(char *a, char *b, char *c, size_t len){
int k = 0;
for (int i = 0; i < strlen(a); i++){
for (int j = 0; j < strlen(b); j++){
if (a[i] == b[j]){
printf("%c", a[i]);
for(size_t i = 0; i < size; i++) {
if(c[i] == a[i])
{
continue;
}
}
if(i < len)
{
c[k] = a[i];
}
else if( i > len )
{
return NULL;
}
k++
}
}
}
x[k] = '\0';
return c;
}
不能給你完整的代碼,但可以用另一種方法幫助你。 簡單的方法是創建新的字符串變量(例如結果),您可以像以前一樣保存常用字符(您可以動態創建它或使用最長字符串的大小在 2 之間),而不是 go 通過一個字符串中的每個元素使用循環,而不是使用 string.h 中的 strchr() function 檢查字符是否在另一個字符串中,如果它在另一個字符串中,再次檢查它是否是結果,如果它不只是將它連接到 strcat() 的結果。 我試圖盡可能清楚地解釋它。 希望能幫助到你:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.