0% found this document useful (0 votes)
5 views5 pages

String Programs

The document contains multiple C++ programs that perform various string operations including finding the length, reversing, counting vowels, converting to uppercase/lowercase, copying, counting words, finding character frequency, and determining the size of a string. Each program prompts the user for input and outputs the result of the respective operation. The examples demonstrate the use of both C++ string and character arrays for string manipulation.

Uploaded by

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

String Programs

The document contains multiple C++ programs that perform various string operations including finding the length, reversing, counting vowels, converting to uppercase/lowercase, copying, counting words, finding character frequency, and determining the size of a string. Each program prompts the user for input and outputs the result of the respective operation. The examples demonstrate the use of both C++ string and character arrays for string manipulation.

Uploaded by

novatechpvtltd61
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Length of a string

#include <iostream>
#include <string>
using namespace std;

int main()
{
string str;

cout << "Enter a string: ";


getline(cin, str); // To input full line including spaces

int len = [Link](); // Finding length

cout << "Length of the string is: " << len;

return 0;
}
OR
#include <iostream>
using namespace std;

int main()
{
char str[100];
int length = 0;

cout << "Enter a string: ";


[Link](str, 100);

while (str[length] != '\0') // Null character check


{
length++;
}

cout << "Length of the string is: " << length;

return 0;
}

Reverse a string
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main()
{
string str;

cout << "Enter a string: ";


getline(cin, str);

reverse([Link](), [Link]());

cout << "Reversed string is: " << str;

return 0;
}
OR
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;

cout << "Enter a string: ";


getline(cin, str);

int length = [Link]();

cout << "Reversed string is: ";

for(int i = length - 1; i >= 0; i--)


{
cout << str[i];
}

return 0;
}
Count vowels
#include <iostream>
#include <string>
using namespace std;

int main()
{
string str;
int count = 0;

cout << "Enter a string: ";


getline(cin, str);

for(int i = 0; i < [Link](); i++)


{
if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' ||
str[i] == 'o' || str[i] == 'u' ||
str[i] == 'A' || str[i] == 'E' || str[i] == 'I' ||
str[i] == 'O' || str[i] == 'U')
{
count++;
}
}

cout << "Number of vowels: " << count;

return 0;
}
Convert to uppercase
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
string str;

cout << "Enter a string: ";


getline(cin, str);

for(int i = 0; i < [Link](); i++)


{
str[i] = toupper(str[i]);
}

cout << "String in uppercase: " << str;

return 0;
}
Convert to lowercase
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
string str;

cout << "Enter a string: ";


getline(cin, str);

for(int i = 0; i < [Link](); i++)


{
str[i] = tolower(str[i]);
}

cout << "String in lowercase: " << str;

return 0;
}
Copy string
#include <iostream>
using namespace std;

int main()
{
char str1[100], str2[100];
int i = 0;

cout << "Enter a string: ";


[Link](str1, 100);

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

str2[i] = '\0'; // Add null character at end

cout << "Copied string is: " << str2;

return 0;
}
Count words in a string
#include <iostream>
#include <string>
using namespace std;

int main()
{
string str;
int words = 0;

cout << "Enter a sentence: ";


getline(cin, str);

for(int i = 0; i < [Link](); i++)


{
// Check if current character is not space
// and either it is the first character
// or the previous character is space
if(str[i] != ' ' && (i == 0 || str[i - 1] == ' '))
{
words++;
}
}

cout << "Number of words: " << words;

return 0;
}
Frequency of a character
#include <iostream>
#include <string>
using namespace std;

int main()
{
string str;
char ch;
int count = 0;

cout << "Enter a string: ";


getline(cin, str);

cout << "Enter the character to find: ";


cin >> ch;

for(int i = 0; i < [Link](); i++)


{
if(str[i] == ch)
{
count++;
}
}

cout << "Frequency of '" << ch << "' is: " << count;

return 0;
}
Size of a string
#include <iostream>
#include <string>
using namespace std;

int main()
{
string str;

cout << "Enter a string: ";


getline(cin, str);

int size = [Link](); // Finding size of string

cout << "Size of the string is: " << size;

return 0;
}

You might also like