C/String Trinangle in C
Expert: Prince M. Premnath - 2/17/2008
QuestionQUESTION: Hi Prince M. Premnath,
Good to have you back. Here is a programe you once solved on this site and since then, i have been battling to understand every line of what the programme does.
question:
write a programme displaying the name ANIRBAN in the form:
A
A N I
A N I R B
A N I R B A N
and you wrote the folowing code for this programe NB(first,compile this programme to see it works before answering my question please):
#include<stdio.h>
#include<string.h>
int main(void){
char *str = "ANIRBAN";
int i, j, k, m, n;
n = (strlen(str)/3) + 1;
for(i=0; k=n; i<=strlen(str); i+2, k--)
{
for(m=0; m<=k; m++)
printf("");
for(j=0; j<=i; j++)
printf("%c", *(str + j));
printf("\n");
}
return 0;
}
Please, explain the reasoning and algorithm of every line in this source code please. I am really having problems with understanding what and why some of the lines were written.
Henry
ANSWER: Hi dear henry !
Nothing wrong with my programme , where as you committed some mistakes given below the original version of the programme copied from the site !
I feel happy if i spot you what mistakes you have done !
for(i=0; k=n; i<=strlen(str); i 2, k--)
1. Use of ';' instead of ','
2. use of i 2 instead of i = 2
#include<stdio.h>
void main()
{
char * str = "ANIRBAN";
int i , j , k , l , n;
n = (strlen(str) / 3) 1;
for( i = 0 , k = n ; i <= strlen(str); i+=2 , k--)
{
for( l = 0 ; l<=k ; l )
printf(" ");
for( j = 0 ; j <= i ; j )
printf("%c" , *(str+j));
printf("n");
}
}
Take a look at the triangle what we expect as an output , here i marked using the symbol '*'
instead of spaces
Take a careful look at this triangle ! for the 1st row it takes 3 '*'s to fill the gap ( here i used '*'s for your understanding purpose) and the number reduced by 1 for up coming rows , the initial number of spaces can be calculated depending on the length of the string , in this programme length of the string hence i used this simple formulae to calculate the initial spaces n = (strlen(str)/3) 1; for the rounding off purpose i added 1 with the result !
***A*** -- Line 1 ( char count 1) i = 1; val of i = 1
**ANI** -- Line 2 ( Char count 3) i = i + 2; val of i = 3
*ANIRB* -- Line 3 ( char count 5) i = i + 2; val of i = 5
ANIRBAN -- Line 4 ( char count 7) i = i + 2; val of i = 7
for( i = 0 , k = n ; i <= strlen(str); i +=2 , k--)
Above for loop variable i gets incremented by 2 since because each and every line the character
count increments by 2 ; K holds number of spaces to fill before printing the actual character sequence ( initially it holds 3 ( as we calculated earlier ) and then gradually decrement by 1 )
This loop takes the responsibility of printing the white spaces depends on the value of k
for( l = 0 ; l<=k ; l )
printf(" ");
as i said earlier 'i ' incremented by 2 to maintain the odd character sequence following loop takes the responsibility of printing the characters
for( j = 0 ; j <= i ; j )
printf("%c" , *(str+ j));
Only thing that confuses you here is *(str +j) ; here i used pointers for the sake of convenience , just one thing i like to clear you now with this pointers !
*(str+0) holds the character A , J takes 0
*(str+1) holds the character N , J takes 1
*(str+2) holds the character I , J takes 2
*(str+3) holds the character R , J takes 3
*(str+4) holds the character B , J takes 4
*(str+5) holds the character A , J takes 5
*(str+6) holds the character N , J takes 6
Hope this might help you out understanding the logic of this programme!
revert me in case of issues !
Thanks and Regards!
Prince M. Premnath
Note : Editor what we are given to answer doesn't support technical answers and usage of special symbols thats why most of the program codes lose their vital operators and possible result with error , kindly regret .
---------- FOLLOW-UP ----------
QUESTION: Hello Prince,
I want to first thank you for ur patience and committment to assisting me in my programming career.I understand every line except for the n = (strlen(str)/3)+1. why did you divide the length by 3 then add 1? what do you really need the length for in the question? I just hope you will have time to answer all the questions i have recently posted to you.
Thanks alot.
Answer Hi dear henry !
Nothing sounds interesting with this formulae n = (strlen(str)/3)+1
rough calculations says thats the number of spaces comes around 1/3 rd of the actual length of the string ! i ve added 1 since 1/3rd length will always results in fractions in order to roundoff the fractions ive did like that !!
i found this Just using trial and error method !!
Thanks and Regards !
Prince M. Premnath