To print triangle or pattern of the string which enter by user, for that first we need to find the length of the string by using string function strlen(). Now suppose user enter a string "programming" So it will be print following trianlge.
p
p r
p r o
p r o g
p r o g r
p r o g r a
p r o g r a m
p r o g r a m m
p r o g r a m m i
p r o g r a m m i n
p r o g r a m m i n g
If you want source code of any other pattern just write down your pattern in comment we will provide source code of that pattern.
Source code to print string pattern or triangle
#include<stdio.h>
void main()
{
int i,j,len=0,k;
char str[50];
clrscr();
printf("Enter a string: ");
scanf("%s",&str);
len=strlen(str);
len=len-1;
for(i=0;i<=len;i++)
{
for(k=i;k<=len;k++)
{
printf(" ");
}
for(j=0;j<=i;j++)
{
printf("%c ",str[j]);
}
printf("\n");
}
getch();
}
void main()
{
int i,j,len=0,k;
char str[50];
clrscr();
printf("Enter a string: ");
scanf("%s",&str);
len=strlen(str);
len=len-1;
for(i=0;i<=len;i++)
{
for(k=i;k<=len;k++)
{
printf(" ");
}
for(j=0;j<=i;j++)
{
printf("%c ",str[j]);
}
printf("\n");
}
getch();
}
Now lets consider another half triangle pattern program.
p
pr
pro
prog
progr
progra
program
programm
programmi
programmin
programming
#include<stdio.h>
void main()
{
int i,j,len=0,k;
char str[50];
clrscr();
printf("Enter a string: ");
scanf("%s",&str);
len=strlen(str);
len=len-1;
for(i=0;i<=len;i++)
{
for(k=i;k<=len;k++)
{
printf(" ");
}
for(j=0;j<=i;j++)
{
printf("%c",str[j]);
}
printf("\n");
}
getch();
}
Now lets consider one another example.
p
pr
pro
prog
progr
progra
program
programm
programmi
programmin
programming
#include<stdio.h>
void main()
{
int i,j,len=0;
char str[50];
clrscr();
printf("Enter a string: ");
scanf("%s",&str);
len=strlen(str);
for(i=0;i<len;i++)
{
for(j=0;j<=i;j++)
{
printf("%c",str[j]);
}
printf("\n");
}
getch();
}
0 comments:
Post a Comment