DS Assignment 2
DS Assignment 2
Sahiwal Campus
(Department of Computer Science)
Assignment 2 Spring 2021
Course
Course Title: Data Structures and Algorithms CSC211 Credit Hours: 4
Code:
Course Instructor: Ms. Tahreem Saeed Programme: BSCS, BSSE, BSI
Semester: 3rd Batch: 22 Section: -Name: Date: April 14th, 2021
Time Allowed: Maximum Marks: 10
Student’s Name: Hareem Aman Reg. No. CUI/ SP20-BSI-002
Important Instructions / Guidelines: /SWL
Read the question paper carefully and answer the questions according to their statements.
Mobile phones are not allowed. Calculators must not have any data/equations etc. in their memory.
Question 1: Write down C/C++ Codes for the following functions of strings
data structure:
1. Strings in C/C++
#include <iostream>
using namespace std;
int main()
{
char str[100];
cout << "Enter a string: ";
cin >> str;
cout << "Protein code: " << str << endl;
return 0;
}
return 0;
}
else
cout << str << endl;
}
int main()
{
string str;
cout<<"Enter a Protein Sequence:"<<endl;
cin>>str;
rearrangeString(str);
return 0;
}
return true;
}
int main()
{
string str;
cout<<"\nEnter something : ";
cin>>str;
if (isNumber(str))
cout << "It is an Integer"<<endl;
else
cout << "It is a String (protein sequence)"<<endl;
return 0;
}
7. Quick way to check if all the characters of a string are same
#include <iostream>
using namespace std;
bool SameCharacters(string s)
{
int n = s.length();
for (int i = 1; i < n; i++)
if (s[i] != s[0])
return false;
return true;
}
int main()
{
string s;
cout<<"Enter Something : ";
cin>>s;
if (SameCharacters(s))
cout << "Same";
else
cout << "Different";
return 0;
}
int main()
{
string strA;
cout<<"Enter protein code 1: ";
cin>>strA;
string strB;
cout<<"Enter protein code 2: ";
cin>>strB;
cout <<"uncommon letter is : "<< ExtraCharcter(strA, strB);
return 0;
}
10. Array of Strings in C++ (3 Different Ways to Create)
(Using pointers)
#include <iostream>
using namespace std;
int main()
{
const char *protein[6] = { "T", "G", "C", "T" , "A" , "C"};
for (int i = 0; i < 6; i++)
std::cout << protein[i] << " ";
return 0;
}
(Using 2D array)
#include <iostream>
using namespace std;
int main()
{
char protein[5][10] = { "T", "G", "C", "T" , "A" };
for (int i = 0; i < 5; i++)
std::cout << protein[i] << " ";
return 0;
}
return second;
}
int main()
{
string str;
cout<<"Enter a protein sequence : ";
cin>> str;
char res = SecondMostFreq(str);
if (res != '\0')
cout << "Second most frequent Nucleotide is : " << res;
else
cout << "No second most frequent Nucleotide";
return 0;
}
12. C++ Program to Sort an array of names or strings
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char str[5][20], t[20];
int i, j;
cout<<"Enter five nucleotides randomly: "<<endl;
for(i=0; i<5; i++)
{
cin>>str[i];
}
for(i=1; i<5; i++)
{
for(j=1; j<5; j++)
{
if(strcmp(str[j-1], str[j])>0)
{
strcpy(t, str[j-1]);
strcpy(str[j-1], str[j]);
strcpy(str[j], t);
}
}
}
cout<<"\n";
cout<<"Nuleotides Sorted in Alphabetical Order : "<<endl;
for(i=0; i<5; i++)
{
cout<<str[i]<<endl;
}
return 0;
}
13. C++ Program to remove spaces from a string
#include<iostream>
#include<string.h>
using namespace std;
int main ()
{ char str[80];
int i=0, len, j;
cout << "Enter a protein sequence : ";
gets(str);
len = strlen(str);
for( i = 0; i < len; i++)
{
if (str[i] == ' ')
{
for (j = i; j < len; j++)
str[j] = str[j+1];
len--;
}
}
cout<<"\n";
cout << "Resultant protein sequence : " << str;
return 0;
}
14. String Class in C++
#include <iostream>
#include <string>
using namespace std;
int main ()
{
return 0;
}
str.append(str2);
cout << "Using append() : ";
cout << str << endl;
}
int main()
{
string str1;
cout<<"Enter protein code 1 : ";
cin>>str1;
string str2;
cout<<"Enter protein code 2 : ";
cin>>str2;
cout << "\nOriginal String : " << str1 << endl;
appendDemo(str1, str2);
return 0;
}
return 0;
}
i = 0;
while (*s)
{
if (strstr(s, oldW) == s) {
strcpy(&result[i], newW);
i += newWlen;
s += oldWlen;
}
else
result[i++] = *s++;
}
result[i] = '\0';
return result;
}
int main()
{
char str[] = "AAA CAA GTG GGT";
char c[] = "GTG";
char d[] = "CAC";
free(result);
return 0;
}