"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain
Showing posts with label String. Show all posts
Showing posts with label String. Show all posts

Thursday, March 4, 2010

WAP to enter a string. Print it in reverse. Also print the number of vowels, consonants, blank spaces.


#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>

void main()
{
char str[100];
int vowel = 0, cons = 0, sp = 0, chr = 0;

for(int i = 0; str[i] != '\0' ; i++)
{

/* count vowels */
if(toupper(str[i])=='A' || toupper(str[i]) == 'E' || toupper(str[i]) == 'I' || toupper(str[i] == 'O' || toupper(str[i]) == 'U' ))
     vowel++;

else
{
/* count spaces */
    if(str[i] == ' ')
        sp++;
    else
       {
/* count comma or stop as other character */
          if(str[i] == '.' || str[i] == ',')
              chr++;
          else
               cons++;
        }
}
}

cout<< "\n The original string = " << str;

cout<< "\n The total number of vowels = " << vowel;
cout<< "\n The total number of consonants = " << cons;
cout<< "\n The total number of balnk spaces = " << sp;
cout<< "\n The total number of other characters like comma or full-stops =" << chr;

int l = strlen(str);

cout<< "\n The string in reverse is ";
for(i=l-1; i>=0 ; i--)
cout<< str[i];

getch();
}

Thursday, February 25, 2010

Write a program that reads an array of strings and count the number of strings of each length that occurs, as well as the total number of strings. The program should then print the mean (average) entirely of (lowercase and lowercase) letters and is terminated by '.'.


#include<iostream.h>
#include<conio.h>
#include <string.h>
#include<stdio.h>


void main()
{
clrscr();


char str[255] = " Hi \n Enter Strings \n Thanks ";


cout<< "\n Enter a String (~ to terminate ): ";
cin.getline(str, 255, '~');


/* Count no. of strings */
int no_f_strings = 0;
for(int i = 0; str[i] != '\0' ; i++)
   if(str[i] == '\n')
      no_f_strings ++;


/*Count total number of strings */
int tot_strings = 0;
for( i = 0 ; str[i] != '\0' ; i++)
{

/*space or new-line or fullstop means new word starts */


if( str[i] == ' ' || str [i] == '\n' || str[i] == '.')
  {tot_strings++;
  while(str[i] == ' ')
   i++;}
 if(str[i] == '\0')
   i--;
}
tot_strings++; //for the last word in the string array


cout<< "\n The  number of strings of each length : " << no_f_strings;


cout<<"\n The total number of strings (words) : " << tot_strings;


int len = strlen(str);


int ucas = 0, lcas = 0, fullst = 0;


for( i=0; str[i] != '\0' ; i++)
{
if(str[i]>='A' && str[i]<='Z')
  ucas++;


if(str[i]>='a' && str[i]<='z')
 lcas++;


if(str[i]=='.')
fullst++;


}


float avg_ucas = (float) (ucase / len);
float avg_lcas = (float) (lcas/len);


cout<< "\n The Mean  of lowercase letters in strings of array  = " <<  avg_lcas;

cout<< "\n The Mean  of uppercase letters in strings of array = " << avg_ucas;


cout<< "\n The total number of  terminating fullstop= " << fullst;



getch();
}

Tuesday, February 23, 2010

Write a C++ program that reads three strings and prints the longest and smallest string.


#include<iostream.h>
#include<conio.h>
#include<string.h>

void main()
{
char str1[30], srt2[30], str3[30];

cout<<"\n Enter 3 strings /Words one-by-one(press enter each time) :";
gets(str1);
gets(str2);
gets(str3);

/* Find Longest String */
if(strcmp(str1, str2) > 0)
{
   if(strcmp(str1,str3)>0)
       cout <<"\n The Longest string is : " << str1;
  else
       cout <<"\n The Longest string is : " << str3;
}
else
{
if(strcmp(str2,str3)>0)
cout <<"\n The Longest string is : " << str2;
else
cout <<"\n The Longest string is : " << str3;
}


/* Find smallest String */


if(strcmp(str1, str2) < 0)

{
if(strcmp(str1,str3)<0)
cout <<"\n The Smallest string is : " << str1;
else
cout <<"\n The Smallest string is : " << str3;
}
else
{
if(strcmp(str2,str3)<0)
cout <<"\n The Smallest string is : " << str2;
else
cout <<"\n The Smallest string is : " << str3;
}

getch();
}

Saturday, February 20, 2010

Write a function that checks whether or not two arrays (of characters ) are identical, that is, whether they have same characters and all characters in corrosponding positions are equal.


#include<iostream.h>
#include<conio.h>
#include <stdio.h>
#include <string.h>


void main()
{
char sen1[50], sen2[50];
void chk_eq(char str1[], char str2[]);
cout<< "\n Enter String1 :";
gets(sen1);


cout<<"\n Enter String 2:";
gets(sen2);


chk_eq(sen1, sen2);


}


void chk_eq(char str1[], char str2[])
{
int l1,l2;


l1 = strlen(str1);
l2 = strlen(str2);


if(l1 == l2)
{
cout<<" \ n Two String are NOT equal ";
getch();
exit(1);
}


for( int i = 0; i<= l1 - 1 ; i++)
{
  if(str1[i] ! = str2[i])
   {


     cout<<" \ n Two String are NOT equal ";
     getch();
     exit(1);
   }
}


cout<< "\n Two Strings are EQUAL lengthwise , characterwise and casewise ";
getch();
}

Thursday, February 18, 2010

Write a program to reverse a string (i) using another array, (ii) without another array.


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

void main()
{

int str[50], rev[50];

cout<< "\n Enter a string :";

 gets(str);
int l = strlen(str);

/* Reverse String using another array */
for( int i = l-1, k = 0 ; i>= 0 ; i--, k++)
rev[k] = str[i];

 rev[k] = '\0';

cout<< "\n Original String is "<< str;
cout<< "\n Reverse String is "<< rev ;
cout<< "\n Reverse String is ";

/* Reverse String without using another array */
for(  i = l-1 ; i>=0 ; i--)
 cout<< str[i];

 getch();

}

Write a program to count the number of words in a string as well as the number of characters other than a space.


#include<iostream.h>
#include<conio.h>
#include <stdio.h>
#include <string.h>

void main()
{
char sen[50];
int word_count = 0, chr = 0, space = 0;

cout<< "\n Enter A String :";

gets(sen);

for(int i = 0; sen[i] != '\0' ; i++)
{ if (sen[i] == ' ')
 space++; }

chr = strlen(sen) - space;
 word_count = space + 1;

cout<< "\n The Total no. of words = " << word_count;

cout << "\n The total characters without spaces = " << chr;

 getch();
}

Friday, January 8, 2010

A menu driven program to enter a text(word / sentance) and convert it into Uppercase , Lowercase, Togglecase as per the user choice.


#include<iostream.h>
#include<conio.h>
#include<string.h>
{
char str[30], choice;
cout<<"\n Enter A String :";
cin.getline(str,30);
int t,i;


do{
cout<<"*************** MENU ********************\n";
cout<<"\n\n 1. UPPERCASE.";
cout<<"\n 2. LOWERCASE.";
cout<<"\n 3. TOGGLECASE.";


cout<<" Enter your choice (1-2-3)";
cin>>t;


switch(t)
{
case 1:
           for( i = 0; str[i] != '\0' ; i++)
                  str[i] = toupper(str[i]);
           cout<< "\n The output =" << str;


           break;
case 2:


          for( i = 0; str[i] != '\0' ; i++)
              str[i] = tolower(str[i]);
          cout<< "\n The output =" << str;


          break;
case 3:
          for( i = 0; str[i] != '\0' ; i++)


         {            
          
            if(isupper(str[i])
               str[i] = tolower(str[i]);
            if(islower(str[i])


               str[i] = toupper(str[i]);
            }
           cout<< "\n The output =" << str;
            break;
default:
             cout<<"\n Invalid choice.";
}


cout<<"\n Do you continue ?(Y/N)";
cin>>choice;
}while(toupper(choice)= ='Y');
}

Thursday, January 7, 2010

A program that reads a string & a character . Each occurrence of the given character (regardless of its case) is converted to opposite case.

#include(iostream.h)
#include(conio.h)
#include(string.h)


void main()
{
char str[30], ch, ch1;


cout<<"\n Enter a String:";
cin.getline(str,30);


cout<<"\n Enter a character :";
cin>>ch;


for(int i =0; str[i]!='\0'; ++i)
{
if(toupper(ch) = = toupper(str[i]))
  {
    if(str[i]>='A' && str[i]<='Z')
         str[i] = str[i] + 32;
     else
         str[i] = str[i] - 32;
}
}


cout<<"\n The Converted String is  "<< str;
getch();
}

count the number of words starting with vowel

/*  Input ::   I am Fine.
     Output :: 2         */



#include<iostream.h>
#include<conio.h>
#include<string.h>

void main()
{
int vc=0;
char sen[25], ch;

cout<< "\n Type A Sentance:";
cin.getline(sen,25);

for(int i =0; sen[i]!='\0'; i++)
{
if(sen[i]==' ')
{
while(sen[i] = =  ' ')  /* remove multiple space */
i++;

ch = toupper(sen[i]);

switch(ch)
{
case 'A':
case 'E':
case 'O':
case 'I':
case 'U':
                  vc++;
}
 }
}
cout<<"\n  The total number of words starting with a vowel is : "<< vc;

getch();
}

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.