Computer Scientists are Pretty Pessimistic

Tuesday 29 September 2015

Print a sentence in a frame.



CODE : :


#include <stdio.h>
#include <string.h>
int main ()
{
    int b[150],i,j=0,k,n,c=0,l,big=0,p=0,w=0;  //i,k,n=loop control
    char a[1000];
    printf("Enter a string: ");
    gets(a);
    l=strlen(a);
    for(i=0;i<=l;i++)
    {
        if(a[i]>='a'&&a[i]<='z'||a[i]>='A'&&a[i]<='Z')
            c++; //c=count letter in a word

        else if(a[i]==' '||i==l)
        {
            b[j]=c; //j=array control // b integer array, to put number of letter in a word
            c=0;
            j++;
        }
    }
    for(i=0;i<j;i++)
        {
            if(b[i]>big)
                big=b[i]; //maximum letter in a word (exp: my name is imtiyaz; here imtiyaz= 7)
        }                  // here big =7, for imtiyaz.
    for(i=1;i<=j+2;i++)
    {
        if(i==1||i==(j+2))
            {
                for(k=0;k<big+4;k++) //print (7+4)=11 star in first line
                    printf("*");
                printf("\n"); //new line after 1st and last line
            }
        else
        {
            printf("* ");//a space here
            while(a[p]!=' ')
            {
                printf("%c",a[p]);
                w++; //count letter my=2,name=4,is=2
                p++;
                if(p==l) //print char array p=0,p=1,p=2,p=3
                    break;
            }
                for(n=0;n<(big-w);n++) //print space after printing a word (* my      *), cause(7-2)=5space print
                    printf(" ");
                printf(" *"); //a space here
                p++; // increment p;
                w=0;
                printf("\n"); //new line after every line
        }
    }
    return 0;
}

No comments:

Post a Comment