Function to Center Align Text in C++ Program


Though it is simple and really intuitive to align text in center of screen through C++ program, I found many new students are confused and keep looking for any built in function. Thus, here I present the source code of function that will center the text supplied in center of screen (horizontally).

function centerText(char* s){

len=strlen(s);

int pos=(int)((80-len)/2);

for (int i=0; i<pos; i++)

   cout<< “ ”;

cout<<s;

}


You can place this function in global scope of your program and use it to display any text at the center of screen. For example:

centerText(“This is my sample text to center align”);

centerText(“It is displayed correctly);

Look at the complete program below to better understand the uses of this function:

#include<iostream>
#include<string>
#include<sstream>

#define  orgName “ABC & Sons Co. Ltd”
#define orgAddress “Lazimpat, Kathmandu, Nepal”

using namespace std;

void centerText(char* s);

int main(){
     centerText(“World where everything is granted!”);
     centerText(orgName);
     centerText(orgAddress);
     cout<<endl;
     system(“Pause”);
     return 0;
     }
void centerText(char* s){
     int l=strlen(s);
     int pos=(int)((80-l)/2);
     for(int i=0;i<pos;i++)
        cout<<” “;
     cout<<s<<endl;
     }

The output of the program above is displayed as:

center-screen-shoot_CapturedArea

Tagged As: , , , , ,

13 Responses to “Function to Center Align Text in C++ Program”

  • [...] This post was mentioned on Twitter by ICT Trends, Kannan B. Kannan B said: Function to Center Align Text in C++ Program: Though it is simple and really intuitive to align text in center of … http://bit.ly/aRAgla [...]

  • army costumes on August 1, 2010

    Thanks for the info! ive been looking for this code for a while now. Keep up the good work!

  • vinusha on August 2, 2010

    Thanks for this post. Very much helpful to those who want to know about the techniques in C++.

  • SQL Training on August 3, 2010

    Thanks a lot for this tutorial, I have just started learning C++ and this code will certainly help me. Thanks again

  • joness on August 3, 2010

    Your writing is very beautiful, I congratulate you. I’ve given you a very nice article blog’dada there. I encourage you to visit.
    http://kaancan.co.cc/
     

  • Long Island Web Design on August 4, 2010

    Back when I was just starting using C++ I was also confused how to center align texts. Most professors in universities don't teach the center function for C++. I needed to search for it on the internet. Thanks for this post, it brings back memories in college.

  • Sounds interesting man!!!First  i thought that placing a text in the center is possible only in C language and in Java language by using " symbol in output statements. Thanks for sharing this info.

  • Onion on August 4, 2010

    Thanks for the post. This is beneficial especially to people like me who's into programming stuff (:

  • wireless security systems on August 16, 2010

    Thanks fot such a clear understandable code. You really know how to explain.

  • insurance software on January 2, 2011

    Thanks a lot for this article, I have just started using C++ and I think this will very much help. Thanks
    .

  • I have just started learning C++ and this code will certainly help me.This trick helps me to understand C++ very well.I expect much more form you!!!!!!!!!!!!
    Thanks once again for your work

  • trouble maker on February 10, 2011

    but how can i center this?
    centerText("   Time and date: %s",asctime(timeinfo)); <- a string with a value from a variable :(
    im a noob here :( sory :(

  • MeduZa on August 27, 2011

    that is not C++ that is C

    the C++ way to center text is using iomanip library

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Review this blog on Bloggers.com