In: Mechanical Engineering
In C
Exercise 8.1 A word is said to be “abecedarian” if the letters in the word appear in alphabetical order. For example, the following are all 6-letter English abecedarian words.
abdest, acknow, acorsy, adempt, adipsy, agnosy, be?st, behint, beknow, bijoux, biopsy, cestuy, chintz, de?ux, dehors, dehort, deinos, diluvy, dimpsy
a. Describe an algorithm for checking whether a given word (String) is abecedarian, assuming that the word contains only lower-case letters. Your algorithm can be iterative or recursive.
b. Implement your algorithm in a function called IsAbecedarian().
#include <stdio.h>
#include <string.h>
#include <conio.h>
int IsAbecedarian(char s[100],int n)
{
printf("\n String is:");
puts(s);
int flag=0;
int t,a;
for(int i=1;i<=n-1;i++) //Here i=1 so as to start from 2nd charachter(comparison)
{ //Here i goes to n-1 as i starts from 1. At i=n junk value.
t=s[i];
printf("\n Ascii value: %d",t); //Used for code checking.
for(int j=0;i>j;j++) //Inner loop is used to compare all previous charachters
{ //upto the current charachter s[i].
a=s[j];
printf("\n Ascii value: %d",a); //Used for code working.
if(t<a) //Ascii value comparison for every charachter.
{
flag=1;
break; //If one Ascii value exceeds the break loop
}
}
}
return flag;
}
int main()
{
clrscr();
printf("\n Enter String data:");
char s[100];
gets(s);
int flag;
flag=IsAbecedarian(s,strlen(s)); //Sending string data and size.
if(flag==0)
printf("\n The data is Abecedarian");
else
printf("\n The data is not Abecedarian");
return 0;
}
THIS IS THE CODE. I AM ALSO ATTACHING TEST RUN PICS. DO CHEK THEM. HOPE IT HELPS.(STRINGS USED DURING RUN: behint & abdeabc)