C 查找所有字符串中存在的公共字符
比较每个字符串,找出所有字符串中常用小写字母的数目。 每个字符串由一个从“a”到“z”的小写字母表示 输入示例: 示例输出: 代码:C 查找所有字符串中存在的公共字符,c,string,C,String,比较每个字符串,找出所有字符串中常用小写字母的数目。 每个字符串由一个从“a”到“z”的小写字母表示 输入示例: 示例输出: 代码: 不确定为什么要在这里对前两个字符串进行特殊处理?伪代码中的类似方法如何: - create a set of characters, name it letters_in_all_strings - add every lowercase letter to letters_in_all_strings - for each input string - cr
不确定为什么要在这里对前两个字符串进行特殊处理?伪代码中的类似方法如何:
- create a set of characters, name it letters_in_all_strings
- add every lowercase letter to letters_in_all_strings
- for each input string
- create a set of characters, name it letters_in_this_string
- add every character in the input string to letters_in_this_string
- remove all letters from letters_in_all_strings that are not present in letters_in_this_string
- print out the size of letters_in_all_strings
您可以使用由0和1组成的数组(按字符索引)来实现C中的一组字符,也可以使用glib。或者考虑使用更现代的编程语言?请考虑一个更具描述性的标题。这个标题可以应用于数千个问题。char var[0][100];可能不是你想要的。看起来像学校的项目。如果你想在编程方面取得成功,那么就要努力完成这个思维过程。干净利落,切中要害。@BLUEPIXY你能解释var[i][ch-'a']@user3315556 ch-'a':abcdefghij…->0123456789…,[0..25]记录对应字符的存在情况,并将其替换为字符a-z的索引0-25。如果ch='b'so var[0]['b'-'a']=1'b'-'a'从b到a?@user3315556'b'-'a'为1'在ASCII字符代码中,b'比a'的代码大1。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
int n;
scanf("%d",&n);
char str[n][100];
char var[n][26];
memset(&var[0][0], 0, sizeof(var));
for(int i=0; i<n; i++) {
scanf("%99s", str[i]);
char ch;
for(int j=0; ch=str[i][j]; ++j){
if(islower(ch)){
var[i][ch-'a']=1;//depend on the sequence of character codes
}
}
}
int x = 0;
for(int i=0; i<26; ++i){
int num = 0;
for(int j=0;j<n;++j)
if(var[j][i])
++num;
if(num==n)//all string has character of ('a'+i)
++x;
}
printf("%d\n",x);
return 0;
}
#include<stdio.h>
#include<string.h>
int main() {
int n;
scanf("%d\n",&n);
char str[n][100];
char var[0][100];
for(int i=0; i<n; i++) { // strings
scanf("%99s/n",str[i]);
}
for(int i=0;i<100;i++) { // comparison of first 2 strings
for(int k=0;k<100;k++)
if(str[0][i]==str[1][k])
for(int j=0;j<strlen(str[0]);j++) {
var[0][j]=str[0][j]; // storing the common letters in a var array
}
}
for(int l=0; l<strlen(str[1]); l++) { // comparing letters in var array with the letters of all other strings
int x;
if(var[0][l]==str[l+2][l]);
x=strlen(var[0]); // counting the common letters
printf("%d\n",x);
}
return 0;
}
- create a set of characters, name it letters_in_all_strings
- add every lowercase letter to letters_in_all_strings
- for each input string
- create a set of characters, name it letters_in_this_string
- add every character in the input string to letters_in_this_string
- remove all letters from letters_in_all_strings that are not present in letters_in_this_string
- print out the size of letters_in_all_strings
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
int n;
scanf("%d",&n);
char str[n][100];
char var[n][26];
memset(&var[0][0], 0, sizeof(var));
for(int i=0; i<n; i++) {
scanf("%99s", str[i]);
char ch;
for(int j=0; ch=str[i][j]; ++j){
if(islower(ch)){
var[i][ch-'a']=1;//depend on the sequence of character codes
}
}
}
int x = 0;
for(int i=0; i<26; ++i){
int num = 0;
for(int j=0;j<n;++j)
if(var[j][i])
++num;
if(num==n)//all string has character of ('a'+i)
++x;
}
printf("%d\n",x);
return 0;
}