0% found this document useful (0 votes)
20 views8 pages

string programs example

Uploaded by

depanimokshith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views8 pages

string programs example

Uploaded by

depanimokshith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Programs on strings in c++

1. Write a program to count number of words in string.


#include<iostream>
using namespace std;
int main( )
{
char str[80];
cout << "Enter a string: ";
cin.getline(str,80);
int words = 0; // Holds number of words
for(int i = 0; str[i] != '\0'; i++)
{
if (str[i] == ' ') //Checking for spaces
{
words++;
}
}
cout << "The number of words = " << words+1 << endl;
return 0;
}
2. Write a program to concatenate one string contents to another.
#include<iostream>
using namespace std;
int main( )
{
char str1[80], str2[80];
cout<<"Enter first string: ";
cin.getline(str1, 80);
cout<<"Enter second string: ";
cin.getline(str2, 80);
int l = 0; //Hold length of first string

//Find length of first string.


for(l = 0; str1[l] != '\0'; l++);

//Adding second string content in first


for(int i = 0; str2[i] != '\0'; i++)
{
str1[l++] = str2[i];
}

str1[l] = '\0';
cout << "\nThe first string after adding second string content:\n\n" << str1;

return 0;
}

3. Comparing two strings


#include<iostream>
using namespace std;

int main( )
{
char str1[80], str2[80];

cout<<"Enter first string: ";


gets(str1);

cout<<"Enter second string: ";


gets(str2);

int i;
for (i = 0; str1[i] == str2[i] && str1[i]!= '\0' && str2[i] != '\0'; i++);

if(str1[i] - str2[i] == 0)
cout << "Strings are equal";
else
cout << "Strings are not equal";

return 0;
}
4. Palindrome or not
#include<iostream>
using namespace std;

int main( )
{
char str[80];

cout<<"Enter string: ";


cin.getline(str, 80);

int l; //Hold length of string


//finding length of string
for(l = 0; str[l] != '\0'; l++);

//Comparing first element with last element till middle of string


int i;
for(i = 0; (i < l/2) && (str[i] == str[l - i - 1]); i++);

if(i == l/2)
cout << "Palindrome";
else
cout << "Not a palindrome";

return 0;
}

Write a program to find a substring within a string. If found display its starting position.
Source Code
#include<iostream>
using namespace std;

int main( )
{
char str1[80], str2[80];

cout<<"Enter first string: ";


cin.getline(str1, 80);

cout<<"Enter second string: ";


cin.getline(str2, 80);

int l = 0; //Hold length of second string

//finding length of second string


for(l = 0; str2[l] != '\0'; l++);

int i, j;

for(i = 0, j = 0; str1[i] != '\0' && str2[j] != '\0'; i++)


{
if(str1[i] == str2[j])
{
j++;
}
else
{
j = 0;
}
}

if(j == l)
cout<<"Substring found at position "<< i - j + 1;
else
cout<<"Substring not found";

return 0;
}

Write a program to reverse a string.


Source Code
#include<iostream>
using namespace std;

int main( )
{
char str[80];

cout<<"Enter string: ";


cin.getline(str, 80);

int l; //Hold length of string


for(l = 0; str[l] != '\0'; l++); //finding length of string

int temp;
for(int i = 0, j = l - 1; i < l/2; i++, j--)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}

cout << "Reverse string: " << str << endl;

return 0;
}
Programs with User Defined Functions

Finding length of the string using user defined functions


#include<iostream>
using namespace std;
int FindLength(char str[])
{
int len = 0;
while (str[len] != '\0')
len++;
return (len);
}
int main() {
char str[100];
int length;
cout<<"\nEnter the String : ";
cin.getline(str,100);
length = FindLength(str);
cout<<"\nLength of the String is :"<<length;
return(0);
}

Programs with User Defined Functions and Pointers

Program to copy one string to other using pointers


#include<iostream>
using namespace std;
void stringCpy(char* s1,char* s2)
{
int i=0;
while(s2[i]!='\0')
{
s1[i]=s2[i];
i++;
}
s1[i]='\0'; /*string terminates by NULL*/
}
int main()
{
char str1[100],str2[100];
cout<<"Enter string 1: ";
cin.getline(str1,100);
stringCpy(str2,str1);
cout<<"String 1: "<<str1<<endl;
cout<<"String 2: "<<str2<<endl;
return 0;
}

Program to convert string into lowercase and uppercase without using library
function in C++
#include<iostream>
using namespace std;
void stringLwr(char *s)
{
int i=0;
while(s[i]!='\0')
{
if(s[i]>='A' && s[i]<='Z'){
s[i]=s[i]+32;
}
++i;
}
}

void stringUpr(char *s)


{
int i=0;
while(s[i]!='\0')
{
if(s[i]>='a' && s[i]<='z'){
s[i]=s[i]-32;
}
++i;
}
}
int main()
{
char str[100];
cout<<"Enter any string : ";
cin.getline(str,100);
cout<<"inputted string is:"<<str<<endl;
stringLwr(str);
cout<<"String after stringLwr :"<<str<<endl;

stringUpr(str);
cout<<"String after stringUpr :"<<str<<endl;
return 0;
}

Program to calculate length of string using pointers


#include<iostream>
using namespace std;
int stringLength(char* txt)
{
int i=0,count=0;
while(txt[i]!='\0'){
count+=1;
i++;
}
return count;
}
int main()
{
char str[100]={0};
int length;
cout<<"Enter any string: ";
cin.getline(str,100);
length=stringLength(str);
cout<<"String length is : "<<length<<endl;
return 0;
}

OR

#include<iostream>
using namespace std;
int stringLength(char* txt)
{
int i=0,count=0;
while(*txt !='\0'){
count+=1;
*txt++;
}
return count;
}
int main()
{
char str[100]={0};
int length;
cout<<"Enter any string: ";
cin.getline(str,100);
length=stringLength(str);
cout<<"String length is : "<<length<<endl;
return 0;
}

You might also like