string programs example
string programs example
str1[l] = '\0';
cout << "\nThe first string after adding second string content:\n\n" << str1;
return 0;
}
int main( )
{
char str1[80], str2[80];
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];
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];
int i, j;
if(j == l)
cout<<"Substring found at position "<< i - j + 1;
else
cout<<"Substring not found";
return 0;
}
int main( )
{
char str[80];
int temp;
for(int i = 0, j = l - 1; i < l/2; i++, j--)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
return 0;
}
Programs with User Defined Functions
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;
}
}
stringUpr(str);
cout<<"String after stringUpr :"<<str<<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;
}