如何在C中查找数组中的重复字母
How to find duplicate letter in array in C
我正在制作一个程序,它要求用户输入一个参数 (argv[1]),其中参数是重新排列的字母表中的每个字母,但是用户喜欢它。有效输入的示例是“YTNSHKVEFXRBAUQZCLWDMIPGJO”和“JTREKYAVOGDXPSNCUIZLFBMWHQ”。无效输入的示例将是“VCHPRZGJVTLSKFBDQWAXEUYMOI”和“ABCDEFGHIJKLMNOPQRSTUYYYYY”,因为在各自的示例中有重复的 'V' 和 'Y'。
到目前为止我所知道的是,您可以像下面这样遍历整个参数
for (int j = 0, n = strlen(argv[1]); j < n; j++)
{
//Place something in here...
}
但是,我不太清楚在查找重复项时这是否是正确的方法?此外,我希望答案尽可能简单,知道时间和 cpu 使用不是优先事项,因此“最佳”算法不一定是我要找的算法。
假设您所有的字母都是大写字母,您可以使用散列 table 使该算法在 O(n)
时间复杂度下运行。
#include<stdio.h>
#include<string.h>
int main(int argc, char** argv){
int arr[50]={};
for (int j = 0, n = strlen(argv[1]); j < n; j++)
{
arr[argv[1][j]-'A']++;
}
printf("duplicate letters: ");
for(int i=0;i<'Z'-'A'+1;i++){
if(arr[i]>=2)printf("%c ",i+'A');
}
}
这里我们将数组 arr
初始化为零。该数组将记录每个字母的出现次数。
然后我们寻找出现 2 次或更多次的字母,这些是重复的字母。
同样使用相同的数组,您可以检查所有字母是否至少出现一次以检查它是否是排列
试试吧。
#include <stdio.h>
#include <stddef.h>
int main()
{
char * input = "ABCC";
size_t ascii[256] = {0, };
char * cursor = input;
char c = '[=10=]';
while((c=*cursor++))
{
if(ascii[c] == 0)
++ascii[c];
else
{
printf("Find %c has existed.\n", c);
break;
}
}
return 0;
}
我不知道这是否是您正在寻找的代码类型,但这就是我所做的。它在给定的字符串集中查找重复项。
#include <stdio.h>
#include <stdlib.h>
#define max 50
int main() {
char stringArg[max];
int dupliCount = 0;
printf("Enter A string: ");
scanf("%s",stringArg);
system("cls");
int length = strlen(stringArg);
for(int i=0; i<length; i++){
for(int j=i+1; j<length; j++){
if(stringArg[i] == stringArg[j]){
dupliCount +=1;
}
}
}
if(dupliCount > 0)
printf("Invalid Input");
printf("Valid Input");
}
我正在制作一个程序,它要求用户输入一个参数 (argv[1]),其中参数是重新排列的字母表中的每个字母,但是用户喜欢它。有效输入的示例是“YTNSHKVEFXRBAUQZCLWDMIPGJO”和“JTREKYAVOGDXPSNCUIZLFBMWHQ”。无效输入的示例将是“VCHPRZGJVTLSKFBDQWAXEUYMOI”和“ABCDEFGHIJKLMNOPQRSTUYYYYY”,因为在各自的示例中有重复的 'V' 和 'Y'。
到目前为止我所知道的是,您可以像下面这样遍历整个参数
for (int j = 0, n = strlen(argv[1]); j < n; j++)
{
//Place something in here...
}
但是,我不太清楚在查找重复项时这是否是正确的方法?此外,我希望答案尽可能简单,知道时间和 cpu 使用不是优先事项,因此“最佳”算法不一定是我要找的算法。
假设您所有的字母都是大写字母,您可以使用散列 table 使该算法在 O(n)
时间复杂度下运行。
#include<stdio.h>
#include<string.h>
int main(int argc, char** argv){
int arr[50]={};
for (int j = 0, n = strlen(argv[1]); j < n; j++)
{
arr[argv[1][j]-'A']++;
}
printf("duplicate letters: ");
for(int i=0;i<'Z'-'A'+1;i++){
if(arr[i]>=2)printf("%c ",i+'A');
}
}
这里我们将数组 arr
初始化为零。该数组将记录每个字母的出现次数。
然后我们寻找出现 2 次或更多次的字母,这些是重复的字母。
同样使用相同的数组,您可以检查所有字母是否至少出现一次以检查它是否是排列
试试吧。
#include <stdio.h>
#include <stddef.h>
int main()
{
char * input = "ABCC";
size_t ascii[256] = {0, };
char * cursor = input;
char c = '[=10=]';
while((c=*cursor++))
{
if(ascii[c] == 0)
++ascii[c];
else
{
printf("Find %c has existed.\n", c);
break;
}
}
return 0;
}
我不知道这是否是您正在寻找的代码类型,但这就是我所做的。它在给定的字符串集中查找重复项。
#include <stdio.h>
#include <stdlib.h>
#define max 50
int main() {
char stringArg[max];
int dupliCount = 0;
printf("Enter A string: ");
scanf("%s",stringArg);
system("cls");
int length = strlen(stringArg);
for(int i=0; i<length; i++){
for(int j=i+1; j<length; j++){
if(stringArg[i] == stringArg[j]){
dupliCount +=1;
}
}
}
if(dupliCount > 0)
printf("Invalid Input");
printf("Valid Input");
}