Tuesday, May 24, 2016

C program to find whether given string or sentence is armstrong or not

#include<stdio.h>
#include<string.h>
#include<math.h>
void main()
{
//str1 is input string here you can input here using your choice
 char str1[50]="hey its its hey",str2[50]="",strtemp[50];
 printf("Input string is %s",str1)
 strlwr(str1);
 int s[50],l[50],c=0;
 for(int i=0;i<=strlen(str1);i++)
  {
   if(str1[i]>=97&&str1[i]<=122)
    continue;
   else
    {
     if(c==0)
      {
       s[c]=0;
       l[c]=i-1;
       c++;
      }
     else
      {
       s[c]=l[c-1]+2;
       l[c]=i-1;
       c++;
      }
    }
  }
 c=c-1;
 for(i=c;i>=0;i--)
  {
   int temp=0;
   if((i!=c))
    {
     strtemp[temp]=' ';
     temp++;
    }
   for(int j=s[i];j<=l[i];j++)
    {
     strtemp[temp]=str1[j];
     temp++;
    }
   strtemp[temp]='\0';
   strcat(str2,strtemp);
  }
 printf("%s",str2);
 if((strcmp(str1,str2)==0))
  {
   printf("\nString are armstrong");
  }
 else
  {
   printf("\nString is not armstrong");
  }
}

output
Input string is hey it's it's hey
String are armstrong

No comments:

Post a Comment