First of all Hello to all of you.
I am new to programming(about a week since I started learning it), a little young(12 years old) and totally interedtes in its thrill.

Second of all I would like to clear the superstition that students join this forum as they want you all GEEKS(love the word) to solve their home works. Well I also go to an institute known as Aptech however, whenver my faculty gives me homework. I do it myself. This is the first time I am asking you for help as my own teacher got a little confused in this sum.

Well I had to create a code to print the following output:
ABCDEFEDCBA
ABCDE EDCBA
ABCD DCBA
ABC CBA
AB BA
A A

Well after some tweaking I was able to do some of it(just got wrong and got it printed it like this):
ABCDEFEDCBA
ABCDE DCBA
ABCD CBA
ABC BA
AB A
A
I again did some tweaking and made something like this:
ABCDEFEDCBA
ABCDE EDCBA
ABCD DCBA
ABC CBA
AB BA
A
I dont know what the problem was. So I made an obvious type of tweaking and I got my output. I am thinking is my code too complicated or obvious. Here is my code. Would you all GEEKS help me:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main()
{
    int i, j, k, n, x, y, l;
    y = 69;
    l = 0;
    for (i = 1; i <=6; i++)
    {
        for (j = 65; j <=71 - i; j++)
        {
            printf("%c", j);
        }
        for (k = 1; k < i; k++)
        {
            printf(" ");
        }
        for (n = 1; n < i - 1; n++)
        {
            printf(" ");
        }

        for (x = 70-i; x >= 65; x--)
        {
            if (i == l && x == y)
            {
                printf("%c", y + 1);
            }
            printf("%c", x);
            if ( i == 1)
            {
                l = 1;
            }
         }
         if (i==6)
         {
             printf("%c", 65);
         }

        y--;
        l++;
        printf("\n");
    }
    getch();
}

P.S. Did I explain my qeustion correctly, or was I too confusing.

Sorry for as the spaces did not appear correctly. Here is what I wanted to do(spaces replaced with asterix);
ABCDEFEDCBA
ABCDE*EDCBA
ABCD***DCBA
ABC*****CBA
AB*******BA
A*********A

I wouldn't go that way with your logic. Don't use ascii numbers for your letters, unless you really need to. Use char's for letters, and stick with 'A', 'B', etc., instead - much easier to read and intuitively understand.

A couple things to get figured out:

1) You need to find the largest char to set as your remove char, in case the instructor (or you), wants to do this with a larger string that goes up to 'G' or beyond.

2) You'll need to print up strlen(c)/2+1 rows. So there's an outer for loop.

3) Your inner for loop

#include <stdio.h>


int main() {
  int i, j, len; 
  char c[15] = { "ABCDEFGFEDCBA" };
  char remove = 'A';

  printf("\n\n%s", c);
  len = strlen(c);

  for(i=0;i<len;i++) {   //find the greatest char
    if(c[i] > remove)
      remove = c[i];     //set it as the remove char
  }
  for(i=0;i<len/2+1;i++) { //for each row to be printed
    for(j=0;j<len;j++) {   //check each char in c[]
      if(c[j] == remove)   //and if it's the remove char
        c[j]=' ';          //replace it with a space
    }
    --remove;              //decrement the remove char
    printf("\n%s", c);
    
  }
  
  printf("\n\n\t\t\t     press enter when ready");

  i = getchar(); ++i;
  return 0;
}

;)

commented: Thanks +0

Add
#include <string.h>

to the above. My compiler does it automatically, so I forget to add it sometimes.

Umm. The statement of the problem is not complete. If all you need to do is print the letters, then you could just do this: /*...*/ expectedOutput = "..."; printf(expectedOutput); I think the problem is more likely to be: Given a string with an odd number of characters, on each line print the string with 0,1,3,...,allbut2,all characters removed from the middle of the string. ... or it might have been stated so there is no need for the 'odd number of characters'.

I would write one loop that works from 0 up to strlen(input)/2 (thoughtful about edge conditions) and replace the characters at data[midpoint+index] and data[midpoint-index] with a space (or asterisk or ...). Print that. Do it again...
Since data is already modified, you only need to fiddle with the ever expanding middle's edges.

commented: Helped me with a friendly response +0

Thanks all, Because Of You All My Problem is solved. ;)

Oh Sorry :-O. Even though my problem is solved, I forgot, again :-O, I dont know anything about strings. Can someone of you please try to make the sum with the given syntax. Further more I have tried a new way. Is it "theek"(Correct In English).

Dont think that your code got wasted, it gave me some ideas, for future. THANKS for them. Love you GEEKS(as I said love the word).

And a side note: My sir has told me to solve the prblem using numbers to represent characters. Sort of a restriction as he hasnt taught me strings.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main()
{
    int i, j, k, m;
    for (i = 0; i <6; i++)
    {
        for (j = 65; j <=70 - i; j++)
        {
            printf("%c", j);
        }
        for (k = 1; k <=2*i-1; k++)
        {
            printf(" ");
        }
        if (i==0)
        {
            for(m=69; m>=65; m--)
            {
                printf("%c", m);
            }
        }
        else
        {
            for(m=70-i; m>=65; m--)
            {
                printf("%c", m);
            }
        }
        printf("\n");
    }
    getch();
}

Is this one better?:S

Hi Shikhin
Did u really gone through and understand what both Adak and griswolf has pointed out.
The question is whether the problem is :
1. Just to write a program that prints some alphabets on the screen in some particular fashion
Or
2. To construct it as an algorithm which is flexible enough to accept variable inputs that will affect the outout of the program likewise.

If its the first case (like what u did it with ur program), its all dealing with static data that is getting printed and the code is not at all flexible (and is definitely not what any programmer will be asked to code).
Real test of a programmer is with the second case (i.e, an algorithm, that's where we start our studies for any programming, right ???). There are (I think) 2 possible cases for constructing this program.

Case 1: A program which will accept any variable string (with odd number characters) as input which has to be printed in that particular fashion (removing odd characters from middle).
Case 2: A program which will accept the number of characters (alphabets, so definitely <= 26) and print the output in that format. So, if input is 6 the starting string will be ABCDEFEDCBA, that is no question of odd character input, and moreover that order of ascending and descending alphabets will be maintained (and it will not be any string input).
The first case solution is already given by Adak.
You can better try with the second case, if u really understand and find it useful to what we are pointing to.
Anywaz, u r doing nice work as a beginner (that's why I hv mentioned all the stuff).

Hi kings_mitra,
Thanks for the compliment and your advise. I also think that my program is toooooo complicated and not correct. That is why i started this thread. However, I did not start strings in my course, so I wanted to do it without strings or even arrays. Can you help me do this this way?
And I should say I couldnt understand the second case. Can you help me?

Hi Kings_mitra,

I understood some of the above stated. I suppose you mean I should make a program which first takes a number suppose 6. It then prints
ABCDEFEDCBA
After that it replaces the 5 and 7 with a space and so on. However, I dont understand how it can be done(without strings or arrays)?

U have got the idea right. As u hv not gone thru strings (char arrays in C) yet, we can construct the same program without strings also.
But one important part of constructing a good algorithm is also choosing the right data structures for efficient design construct (which affects performance in both memory usage and execution time). Here, strings (or char array as u say) fits into the need.

Anywaz, I will check it the way understandable by u.

Hi Shikhin,

I hv not constructed my own code but hv just updated ur's.
Here's ur program with a little change...

#include <stdio.h>
#include <stdlib.h>

int main()
{
        int i, j, k, m;
        int input = 0;

        printf("\nEnter the number of alphabets to be printed : Range [1 - 26] : ");
        scanf("%d", &input);

        if(input < 1 || input > 26)
        {
                printf("\nEnter within the range...\n");
                return 0;
        }
        else
                printf("\nHere's the output tree : \n");

        for (i = 0; i < input; i++)
        {
                for (j = 65; j <= (65 + (input - 1) - i); j++)
                {
                        printf("%c", j);
                }

                for (k = 1; k <= 2*i-1; k++)
                {
                        printf(" ");
                }

                if (i==0)
                {
                        for(m = (65 + (input - 1) - 1); m >= 65; m--)
                        {
                                printf("%c", m);
                        }
                }
                else
                {
                        for(m = (65 + (input - 1) - i); m >= 65; m--)
                        {
                                printf("%c", m);
                        }
                }
                printf("\n");
        }
}

I hv deliberately not provided any comments for the changes I made (though I always used to follow that), as I wanted u to understand the changes by ur own.
Run it with different inputs and understand the little changes.
Two suggestions for u:
1. Once u come across the preprocessor chapter change the value 65 with what is called a "Macro".
Moral is: Do not use hard coded values inside program.
2. Once ur teacher finishes the strings chapter, try and implement it with char arrays.

Anywaz, tell me if u r not getting:?: anything in the above code (though I am expecting u to understand it by urself).
Cheers :cool:

Hi kings_mitra,
Totally understood it after your help. Thanks

Thread Solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.