0% found this document useful (0 votes)
68 views16 pages

DS Assignment 2

The document provides 16 coding questions and solutions related to C++ strings and data structures. It includes questions on defining and initializing strings, string manipulation functions like copy, remove, rearrange characters. It also covers checking string properties, sorting strings, concatenating strings multiple times, and differences between various string concatenation methods in C++. The coding problems and their solutions will help students learn and practice common operations on strings and data structures.

Uploaded by

hareem aman
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)
68 views16 pages

DS Assignment 2

The document provides 16 coding questions and solutions related to C++ strings and data structures. It includes questions on defining and initializing strings, string manipulation functions like copy, remove, rearrange characters. It also covers checking string properties, sorting strings, concatenating strings multiple times, and differences between various string concatenation methods in C++. The coding problems and their solutions will help students learn and practice common operations on strings and data structures.

Uploaded by

hareem aman
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
You are on page 1/ 16

COMSATS University Islamabad

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;
}

2. Storage for Strings in C/C++


#include <iostream>
using namespace std;
int main()
{
char protein[10] = {'A', 'G', 'C', 'T', 'A',’A’,’T’,’G’,’T’,’C’};
cout << protein;
return(0);
}
3. Function to copy string
#include<iostream>
#include <string.h>
using namespace std;
#define PROTEIN_SIZE 9
int main()
{
char src[] = "AATTTGCGTACGATGAC";
char protein[PROTEIN_SIZE];

strncpy(protein, src, 9); //it will print first 9 nucleoside


for (int i = 0; i < PROTEIN_SIZE; i++)
{
cout << protein[i];
}

return 0;
}

4. Removing punctuations from a given string


#include <iostream>
using namespace std;
int main()
{
std::string seq = "AT%#C*%GG$#T**TA$%@C*^&*G%T&*^A&^&";

for (int i = 0, len = seq.size(); i < len; i++)


{
if (ispunct(seq[i]))
{
seq.erase(i--, 1);
len = seq.size();
}
}
cout<<"AT%#C*%GG$#T**TA$%@C*^&*G%T&*^A&^&"<<endl;
cout<<"Protein Sequence: ";
std::cout << seq;
return 0;
}

5. Rearrange characters in a string such that no two adjacent are same


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 26;
struct Key
{
int freq;
char ch;
bool operator<(const Key &k) const
{
return freq < k.freq;
}
};
void rearrangeString(string str)
{
int n = str.length();
int count[MAX_CHAR] = {0};
for (int i = 0 ; i < n ; i++)
count[str[i]-'A']++;
priority_queue< Key > pq;
for (char c = 'A' ; c <= 'Z' ; c++)
if (count[c-'A'])
pq.push( Key { count[c-'A'], c} );
str = "" ;
Key prev {-1, '#'} ;
while (!pq.empty())
{
Key k = pq.top();
pq.pop();
str = str + k.ch;
if (prev.freq > 0)
pq.push(prev);
(k.freq)--;
prev = k;
}
if (n != str.length())
cout << " Not valid String " << endl;

else
cout << str << endl;
}
int main()
{
string str;
cout<<"Enter a Protein Sequence:"<<endl;
cin>>str;
rearrangeString(str);
return 0;
}

6. Program to check if input is an integer or a string


#include <iostream>
using namespace std;
bool isNumber(string s)
{
for (int i = 0; i < s.length(); i++)
if (isdigit(s[i]) == false)
return false;

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;
}

8. Program to find the initials of a name


#include <iostream>
using namespace std;
void initials(const string& name) {
cout<<(char)toupper(name[0]);
for (int i = 0; i < name.length() - 1; i++)
if(name[i] == ' ' || name[i] == '\n')
cout << " " << (char)toupper(name[i + 1]);
}
int main()
{
string name = "guanosine adenosine adenosine thymidine cytidine adenisine thymidine
guanosine cytidine";
cout<<"guanosine adenosine adenosine thymidine cytidine adenisine thymidine
guanosine cytidine"<<endl;
cout<<"Initials of nucleotides are: ";
initials(name);
return 0;
}

9. Find one uncommon character in a string


#include <iostream>
using namespace std;
char ExtraCharcter(string strA, string strB)
{
int res = 0, i;
for (i = 0; i < strA.length(); i++)
{
res ^= strA[i];
}
for (i = 0; i < strB.length(); i++)
{
res ^= strB[i];
}
return ((char)(res));
}

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;
}

(Using string glass)


#include <iostream>
#include <string>
using namespace std;
int main()
{
std::string protein[5] = { "T", "G", "C", "T" , "A" };

for (int i = 0; i < 5; i++)


std::cout << protein[i] << " ";
}

11. C++ program to find second most frequent character


#include <bits/stdc++.h>
using namespace std;
#define NO_OF_CHARS 256
char SecondMostFreq(string str)
{
int count[NO_OF_CHARS] = {0}, i;
for (i = 0; str[i]; i++)
(count[str[i]])++;
int first = 0, second = 0;
for (i = 0; i < NO_OF_CHARS; i++)
{
if (count[i] > count[first])
{
second = first;
first = i;
}
else if (count[i] > count[second] &&
count[i] != count[first])
second = i;
}

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 ()
{

string str1 = "TTGAC";


string str2 = "CAGTT";
string str3;
int len ;
str3 = str1;
cout << "str3 : " << str3 << endl;
str3 = str1 + str2;
cout << "str1 + str2 : " << str3 << endl;
len = str3.size();
cout << "str3.size() : " << len << endl;

return 0;
}

15. C++ program to concatenate a string given number of times


#include <iostream>
#include <string>
using namespace std;
string repeat(string s, int n)
{
string s1 = s;

for (int i=1; i<n;i++)


s += s1;
return s;
}
int main()
{
string s;
cout<<"Enter nucleotides of protein : ";
cin>>s;
int n;
cout<<"How many times : ";
cin>>n;
cout<<"\n";
cout << repeat(s, n) << endl;;
return 0;
}

16. std::string::append vs std::string::push_back() vs Operator += in C++


#include <iostream>
#include <string>
using namespace std;
void appendDemo(string str1, string str2)
{
string str = str1;
str1 += str2;
cout << "Using += : ";
cout << str1 << endl;

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;
}

17. Comparing two strings in C++


#include <iostream>
using namespace std;
void relationalOperation(string s1, string s2)
{
if (s1 != s2)
{
cout << s1 << " is not equal to " << s2 << endl;
if (s1 > s2)
cout << s1 << " is greater than " << s2 << endl;
else
cout << s2 << " is greater than " << s1 << endl;
}
else
cout << s1 << " is equal to " << s2 << endl;
}
int main()
{
string s1;
cout<<"Enter protein code 1 : ";
cin>>s1;
string s2;
cout<<"Enter protein code 2 : ";
cin>>s2;
cout<<"\n";
relationalOperation(s1, s2);
string s3(s1);
string s4(s1);
relationalOperation(s3, s4);
return 0;
}
18. Convert string to char array in C++
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
string s;
cout<<"Enter a Protein code : ";
cin>>s;
cout<<"\n";
cout<<"Entered Protein code is :";
int n = s.length();
char char_array[n + 1];
strcpy(char_array, s.c_str());

for (int i = 0; i < n; i++)


cout << char_array[i];

return 0;
}

19. Extract all integers from string in C++


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

void integer(string str)


{
stringstream ss;
ss << str;
string temp;
int found;
while (!ss.eof())
{
ss >> temp;
if (stringstream(temp) >> found)
cout << found << " ";
temp = "";
}
}
int main()
{
string str = "1: 2 @% 3 xxx 4 apple 5";
integer(str);
return 0;
}

20. C++ program to Replace a word in a text by another given word


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* replaceWord(const char* s, const char* oldW,
const char* newW)
{
char* result;
int i, cnt = 0;
int newWlen = strlen(newW);
int oldWlen = strlen(oldW);
for (i = 0; s[i] != '\0'; i++) {
if (strstr(&s[i], oldW) == &s[i]) {
cnt++;
i += oldWlen - 1;
}
}
result = (char*)malloc(i + cnt * (newWlen - oldWlen) + 1);

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";

char* result = NULL;


printf("Old string: %s\n", str);

result = replaceWord(str, c, d);


printf("New String: %s\n", result);

free(result);
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy