This blog consists of C and C++ programs. C and C++ programming is the basics to learn any programming language. Most of the programs are provided with their respective outputs. This blog also contains Data Structures programs using Object-Oriented Programming (C++). Some of the best books for learning C and C++ programming are also mentioned.

Showing posts with label String. Show all posts
Showing posts with label String. Show all posts

Monday, 19 August 2013

C++ program to check whether the given string is Palindrome or not (3)

Palindrome:
Palindromes are words or phrases that read the same in both directions.
Some examples of common palindromic words: civic, dewed, deified, dad, mom, devoved, hannah, repaper, radar, level, rotor, kayak, reviver, racecar, redder, madam, redder, bob, pop, tot, malayalam, noon, rotator, rotavator, stats, solos, tenet and refer.

Program:
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char s[50],m[50];
int p=1,i;
clrscr();
cout<<"Enter the string: ";
cin>>s;
strcpy(m,s);
strrev(m);
for(i=0;i<=strlen(s)-1;i++)
{
if(*(m+i)!=*(s+i))
{
p=0;
break;
}
}
if(!p)
cout<<"\n"<<s<<" is not a Palindrome";
else
cout<<"\n"<<s<<" is a Palindrome";
getch();
}



Wednesday, 7 August 2013

Program to find the number of vowels, consonants, digits, spaces and other characters

#include<stdio.h>
main()
{
char text[100];
int nv=0, nc=0, nd=0, ns=0, no=0;
void input(char text[], int *pv, int *pc, int *pd, int *ps, int *po);
clrscr();
printf("Enter a line of text: ");
gets(text);
input(text, &nv, &nc, &nd, &ns, &no);
printf("\nNo of Vowels:%d", nv);
printf("\nNo of Consonants:%d", nc);
printf("\nNo of Digits:%d", nd);
printf("\nNo of Spaces:%d", ns);
printf("\nNo of Other Characters:%d", no);
getch();
}
void input(char text[], int *pv, int *pc, int *pd, int *ps, int *po)
{
char c;
int i=0;
while((c=toupper(text[i]))!='\0')
{
if(c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
++*pv;
else if(c>='A'&&c<='Z')
++*pc;
else if(c>='0'&&c<='9')
++*pd;
else if(c==' '||c=='\t')
++*ps;
else
++*po;
++i;
}
}


Tuesday, 6 August 2013

Program to copy the contents of one string to another string using strcpy() function

#include<stdio.h>
void main()
{
char original[50], duplicate[50];
clrscr();
printf("Enter the text: ");
scanf("%s", original);
strcpy(duplicate, original);
printf("\nOrignal string: %s", original);
printf("\nDuplicate string: %s", duplicate);
getch();
}


Program to find the length of the string using strlen() function

#include<stdio.h>
void main()
{
char str[50];
int len;
clrscr();
printf("Enter the text: ");
scanf("%s", str);
len=strlen(str);
printf("Length of the string = %d", len);
getch();
}


Thursday, 25 July 2013

Program to print the String pattern 2

#include<stdio.h>
int main()
{
char *c="ABCDEF";
char *c1="FEDCBA";
int i,j=0;
clrscr();
for(i=6;i>0;i--)
{
printf("%*.*s",i,i,c);
printf("%*.*s\n",i,i,c1+j);
j++;
}
getch();
}


Tuesday, 2 July 2013

Program to find the length of the string without using strlen() function

#include <stdio.h>
void main()
{
char s[1000],i;
clrscr();
printf("Enter a string: ");
scanf("%s",s);
for(i=0; s[i]!='\0'; ++i);
printf("Length of string: %d",i);
getch();
}


Program to print the String pattern 1

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str[100];
int i;
cout<<"Enter the string:";
cin>>str;
for(i=0;i<=strlen(str);i++)
{
cout.write(str,i);
cout<<"\n";
}
getch();
}


Thursday, 27 June 2013

Program to check whether the given string is Palindrome or not (2)

#include <stdio.h>
#include <string.h>
int main()
{
char a[100], b[100];
clrscr();
printf("Enter the string: ");
gets(a);
strcpy(b,a);
strrev(b);
if(strcmp(a,b) == 0)
printf("\nThe string is a Palindrome");
else
printf("\nThe string is not a Palindrome");
getch();
}


Program to check whether the given string is Palindrome or not (1)

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str1[50],rstr[50],ch;
int i=0,k=0;
cout<<"Enter the string:";
cin>>str1;
strcpy(rstr,str1);
k=strlen(str1);
for(i=0;i<k/2;i++)
{
ch=rstr[i];
rstr[i]=rstr[k-i-1];
rstr[k-i-1]=ch;
}
if(strcmp(str1,rstr))
cout<<"\n"<<str1<<" is not a palindrome";
else
cout<<"\n"<<str1<<" is a palindrome";
getch();
}