Answer to Question #53848 in C for madhu

Question #53848
In the Byteland country a string "S" is said to super ascii string if and only if count of each character in the string is equal to its ascii value.
In the Byteland country ascii code of 'a' is 1, 'b' is 2 ...'z' is 26.
Your task is to find out whether the given string is a super ascii string or not.
1
Expert's answer
2015-10-23T03:19:46-0400

#include <stdio.h>
#include <string.h>
#define maxlenght 410

int main(){
char string[maxlenght];
int count[26];
int i, j, n, flag, lenght;

scanf_s("%d", &n);//read test count
for (j = 0; j < n; j++){// for each string

for (i = 0; i < 26; i++){ count[i] = 0;}//reset letter count

fgets(string, maxlenght, stdin);// read string
if (string[0] == '\n') fgets(string, maxlenght, stdin);// if read empty string

lenght = strlen(string); // get string lenght
for (i = 0; i < lenght; i++){// for each letter
if ((string[i] >= 'a') && (string[i] <= 'z')){//if lower case alphabets
count[string[i] - 'a']++;// a - count[0], z - count[25]
}
}

flag = 1;
for (i = 0; i < 26; i++){
if (count[i] != (i + 1) && (count[i] != 0)){ flag = 0; break; }//if the condition isn't satisfied
}
if (flag){
printf("Yes\n");
}
else{
printf("No\n");
}
}
getchar();
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS