i'm currently doing cs50 courses and i was looking for people here to check the function i wrote and tell me if there is a better way to do it.
The first function i wrote is to look for repeatead char in a given string:
bool repeatChar(char key[]){
int array[26] = {0};
int lenght = strlen(key);
for(int i = 0; i < lenght; i++)
{
array[tolower(key[i])-'a']++;
}
for(int k = 0; k < lenght;k++)
{
if(array[k] > 1){
return true;
}
}
return false;
}
Is there a better way to do the code above ? I tried to do it with while but i could not if someone could help me there please.
Now the second function :
bool isChar(char* key){
int lenght = strlen(key);
for(int i = 0; i < lenght; i++)
{
if(!isalpha(key[i]))
{
return false;
}
}
return true;
}
the second function check if the char in a given string is a character or not, but when i put special char '&' or'(' it does not work as it should.
Third Function :
bool validLenghtString(char* key){
if(strlen(key)==26){
return true;
}
return false;
}
the function above only checks if the string lenght is 26 long, is it an overkilli function ?
Last Function :
bool digitCheck(char* key){
int lenght = strlen(key);
for(int i = 0; i < lenght; i++)
{
if(isdigit(key[i])){
return true;
}
}
return false;
}
Pretty explicit function.
Is there a better way to write these functions ?
Aucun commentaire:
Enregistrer un commentaire