Copyright © Programs ++
Design by Dzignine
Showing posts with label uppercase. Show all posts
Showing posts with label uppercase. Show all posts
Saturday, 18 February 2012

Converting an array of string to uppercase

Here is a program to convert an array of string into uppercase. Just like a recent post : "lowercase to uppercase" the following program also uses the standard C++ functions such as toupper() to convert string from lowercase to uppercase.
Now, the process is the same as converting a single string, traversing the entire string  using a loop and converting each
character from lowercase to uppercase(or vice-verse) using toupper() (or tolower() ) and storing the converted characters
into another string.

The only difference that comes in an array of strings is that a loop has to be used to process individual strings sorted in the
array.Consider the string as columns of students in a classroom with each of the student sitting in the front desk as the
starting index of that column.

NOTE : be sure to add a terminating null character ('\0') at the end of each converted string, as this is not done by default by the
toupper() function !!!! :O


Here is an sample program.

// Program to convert an array of string to uppercase

#include <iostream>
#include <cctype>

int main()
{
     char name[5][20];
     char temp[5][20];
     int size;
     // prompts the user to enter the no. of string he/she wants in the array
     std::cout<<" Enter the no. of names you want to insert (max 5 ) : ";
     std::cin>>size;
     std::cin.ignore(); // to clear the memory buffer
     std::cout<<" \n Now enter the names (max 20 characters long ) : \n ";
     for (int i=0; i<size; i++)
     {
          std::cout<<" Name "<<i<<" : ";
          std::cin.getline(name[i],sizeof(name[i]));
     }
     // to convert them into uppercase
     int j=0;

     for (int i=0; i<size; i++) // main loop to traverse the rows
     {
          for (j=0; name[i][j] != '\0'; j++) // sub loop to traverse the individual strings
          {
               temp[i][j] = toupper(name[i][j]); // converts each character to uppercase
          }
          temp[i][j] = '\0'; // adds a terminating null character at the end of each string

     }
     // to show the names in uppercase

     for (int i=0; i<size; i++)
     {
          std::cout<<"\n NAME "<<i<<" : "<<temp[i];
     }
     return 0;
} // end of main


------ OUTPUT ------













Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.


------ Related Posts ------ 
Lowercase to Uppercsae : http://programsplusplus.blogspot.in/2012/01/lowercase-to-uppercase.html

Uppercase to Lowercase : http://programsplusplus.blogspot.in/2012/01/uppercase-to-lowercase.html
Tuesday, 24 January 2012

ASCII values from A-Z

ASCII value of A-Z range from 65 to 90

// Program to print the ASCII value for A-Z characters
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    int x; // to store the ascii codes
    char ch; // to store the characters
    for (int i=65; i<=90; i++)
    {
        ch = i; // puts integer value into char
        cout<<" Character "<<ch<<" ASCII Code "<<i;
        cout<<endl;
    }
    cin.get();
    return 0;
} // end of main   
ASII values of A-Z