Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Lab # 10 – Programming Fundamentals (CPE-121)

Lab Manual # 10

Title: Implementation Of String And Using Built In Function In C++

CLO: CLO-1

Student Name: M.usama saghar Class Roll: CPE-2019-27

Subject Teacher : Engr. Muhammad Baqer


Lab Engineer : Engr. Abdul Rehman
Lab Performed In : Networking Lab (CPED)

LAB POINTS SCORE


Lab Performance [10] 0 1 2 3 4 5
Lab Participation
Lab Activity Completion on the Same Day

Lab Submission[10] 0 1 2 3 4 5
Completeness & Correctness
Required Conclusion & Results

No of Checks
SUB TOTAL
TOTAL SCORE

______________________
Course Instructor / Lab Engineer

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 10 – Programming Fundamentals (CPE-121)
Strings
C++ provides following two types of string representations:
 The C-style character string.
 The string class type introduced with Standard C++
The C-Style Character String:
The C-style character string originated within the C language and continues to be supported
within C++. This string is actually a one-dimensional array of characters which is terminated by
a null character '\0'. Thus a null-terminated string contains the characters that comprise the
string followed by a null.
The following declaration and initialization create a string consisting of the word "Hello". To
hold the null character at the end of the array, the size of the character array containing the
string is one more than the number of characters in the word "Hello."
char greeting[6]={'H','e','l','l','o','\0'};
If you follow the rule of array initialization, then you can write the above statement as follows:
char greeting[]="Hello";
Example
#include<iostream>
usingnamespace std;
int main ()
{
char greeting[6]={'H','e','l','l','o','\0'};

cout<<"Greeting message: ";


cout<< greeting <<endl;

return0;
}
When the above code is compiled and executed, it produces result something as follows:
Greeting message:Hello

C++ supports a wide range of functions that manipulate null-terminated strings

S NO Function & Purpose

1 strcpy(s1, s2);

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 10 – Programming Fundamentals (CPE-121)

Copies string s2 into string s1.

2 strcat(s1, s2);
Concatenates string s2 onto the end of string s1.

3 strlen(s1);
Returns the length of string s1.

4 strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0
if s1>s2.

5 strchr(s1, ch);
Returns a pointer to the first occurrence of character ch in string s1.

6 strstr(s1, s2);
Returns a pointer to the first occurrence of string s2 in string s1.

Following example makes use of few of the above-mentioned functions:


#include<iostream>
#include<cstring>
usingnamespace std;
int main ()
{
char str1[10]="Hello";
char str2[10]="World";
char str3[10];
intlen;
strcpy( str3, str1);
cout<<"strcpy( str3, str1) : "<< str3 <<endl;
strcat( str1, str2);
cout<<"strcat( str1, str2): "<< str1 <<endl;
len=strlen(str1);
cout<<"strlen(str1) : "<<len<<endl;
return0;}
When the above code is compiled and executed, it produces result something as follows:

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 10 – Programming Fundamentals (CPE-121)

strcpy( str3, str1):Hello


strcat( str1, str2):HelloWorld
strlen(str1):10
The String Class in C++:

The standard C++ library provides a string class type that supports all the operations mentioned
above, additionally much more functionality

Example

#include<iostream>
#include<string>
usingnamespace std;
int main ()
{
string str1 ="Hello";
string str2 ="World";
string str3;
intlen;
str3 = str1;
cout<<"str3 : "<< str3 <<endl;
str3 = str1 + str2;
cout<<"str1 + str2 : "<< str3 <<endl;
len= str3.size();
cout<<"str3.size() : "<<len<<endl;
return0;
}
When the above code is compiled and executed, it produces result something as follows:

str3 :Hello
str1 +str2 :HelloWorld
str3.size():10
P-1: Write a C++ program that will find the frequency of a character in the string that is entered
by user.

Your Code:

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 10 – Programming Fundamentals (CPE-121)

#include <stdio.h>
#include <string.h>
int main()
{
char string[100];
int c = 0, count[26] = {0}, x;

printf("Enter a string\n");
gets(string);

while (string[c] != '\0') {


/** Considering characters from 'a' to 'z' only and ignoring others. */

if (string[c] >= 'a' && string[c] <= 'z') {


x = string[c] - 'a';
count[x]++;
}

c++;
}

for (c = 0; c < 26; c++)


printf("%c occurs %d times in the string.\n", c + 'a', count[c]);

return 0;
}

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 10 – Programming Fundamentals (CPE-121)

Paste your output here

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 10 – Programming Fundamentals (CPE-121)
P-2: Write a c++ program that copy the string str1 that is entered by user to str2 without
usingstrcpy() functionality of string.

Your Code:

#include <iostream>
using namespace std;

int main()
{
string s1, s2;

cout << "Enter string s1: ";


getline (cin, s1);

s2 = s1;

cout << "s1 = "<< s1 << endl;


cout << "s2 = "<< s2;

return 0;
}

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 10 – Programming Fundamentals (CPE-121)

Paste your output here

P-3: Write a C++ program to find the number of vowels , consonant , digit and white space in a
string.

Your Code:

#include<iostream>
#include<string.h>
using namespace std;
int main ()
{
char str[50];
int v = 0, c = 0, n = 0, s = 0;
cout << "Enter a string : ";
gets(str);
for (int i = 0; str[i]!='\0'; ++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')
++v;
else if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 10 – Programming Fundamentals (CPE-121)
++c;
else if (str[i] >= '0' && str[i] <= '9')
++n;
else
++s;
}
cout << "Number of vowels : " << v;
cout << "\nNumber of consonants : " << c;
cout << "\nNumber of numbers :" << n;
cout << "\nNumber of special characters : " << s;
return 0;
}

Paste your output here

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 10 – Programming Fundamentals (CPE-121)

P-4: Write a program that compare two user entered string either they are equal or not.

Your Code:

#include <bits/stdc++.h>
using namespace std;

int main()
{

char string1[100], string2[100];

// Get the strings which


// is to be checked
cin >> string1;
cout << "Enter the first string: " << string1;

// Get the strings which


// is to be checked
cin >> string2;
cout << "\nEnter the second string: " << string2;

// Check if both strings are equal


cout << "\nAre both strings same: ";

if (strcmp(string1, string2) == 0)
{
cout << "Yes";

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 10 – Programming Fundamentals (CPE-121)
}
else {
cout << "No";
}

return 0;
}

Paste your output here

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 10 – Programming Fundamentals (CPE-121)

Comments:

In this lab I learn about different functions of strings.


1. Given two strings, the task is to check if these two strings are identical (same) or
not.
2. The program takes a string and counts the number of vowels, consonants,
numbers and special characters in the string and prints them.

Version 1.0 Department of Computer Engineering UCE&T BZU Multan

You might also like