0% found this document useful (0 votes)
127 views

Lab 09@cs 121

This document contains instructions and examples for a programming lab assignment on strings. Students are asked to complete 6 tasks that involve basic string operations and functions in C++, including concatenating, comparing, extracting substrings, and replacing parts of strings. The tasks require getting user input, manipulating strings, and outputting results. Students are reminded to properly format their code, comment it, and submit it by the deadline.

Uploaded by

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

Lab 09@cs 121

This document contains instructions and examples for a programming lab assignment on strings. Students are asked to complete 6 tasks that involve basic string operations and functions in C++, including concatenating, comparing, extracting substrings, and replacing parts of strings. The tasks require getting user input, manipulating strings, and outputting results. Students are reminded to properly format their code, comment it, and submit it by the deadline.

Uploaded by

Saif Hassan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Sukkur IBA University

Department of Computer Science

FUNDAMENTALS OF PROGRAMMING
LAB 09 – Strings

Saif Hassan
Date: 27 th October, 2019

READ IT FIRST
Prior to start solving the problems in this assignments, please give full
concentration on following points.

1. WORKING – This is individual lab. If you are stuck in a problem contact


your teacher, but, in mean time start doing next question (don’t waste
time).

2. DEADLINE – Before Next Lab.

3. SUBMISSION – This assignment needs to be submitted in a soft copy.

4. WHERE TO SUBMIT – Please visit your Google Classroom.

5. HOW TO SUBMIT – rename all .cpp files as task number and make .zip
file, name it as 000-00-0000_lab09.zip, and submit .cpp file ONLY.

KEEP IT WITH YOU!


1. Indent your code inside the main () function. It’s a good practice!

2. It is not bad if you keep your code indented inside the loops, if and
else blocks as well.

3. Comment your code, where it is necessary.

4. Read the entire question. Don’t jump to the formula directly.


Lab08 – Functions CS121 Fundamentals of Programming Instructor: Saif Hassan

Practice
Following code is given for sample code for string and its functions.

1. #include <iostream>
2. #include <string>
3. #include <cstdlib>
4.
5. using namespace std ;
6.
7. int main ()
8. {
9. // creating string variable
10. string a = "22345";
11. char b[] = "a2345";
12.
13. // getting string input friom user
14. getline(cin, a);
15.
16. // calculating size of string
17. cout<<strlen(b)<<endl; // strlen is used for char array
18. cout<<a.size()<<endl; // size used with string var
19. cout<<a.length()<<endl; // size used with string var
20.
21. // using substr function
22. string s = a.substr(0,2);
23. cout<<s<<endl;
24.
25. // conversion from string to int
26. int c = atoi(a.c_str());
27.
28. // isalpha used for checking whether it is abc or not
29. cout<<isalpha(b[0]);
30.
31. // replace str2 with C in str1
32. string str1 = "This is C language";
33. string str2 = "C++";
34. str1.replace(8,1,str2);
35.
36. cout<<endl;
37. system ("PAUSE") ;
38. return 0 ;
39. }

Page 2 of 4
Lab08 – Functions CS121 Fundamentals of Programming Instructor: Saif Hassan

Exercises
Task01 (string without loop)

Write a program to create variable firstname of type string and store


your first name ONLY. Perform following operations:

a) Print string on one line.


b) Print string on one line with spaces.
c) Print each letter of string on new line.
𝑃𝑟𝑖𝑛𝑡 𝑊𝑜𝑤!, 𝑓𝑖𝑟𝑠𝑡𝑛𝑎𝑚𝑒𝑠𝑡𝑎𝑟𝑡𝑠 𝑤𝑖𝑡ℎ (𝐴 𝑜𝑟 𝑎) 𝑎𝑛𝑑 𝑒𝑛𝑑𝑠 𝑤𝑖𝑡ℎ (𝑍 𝑜𝑟 𝑧)
d) 𝑓 (𝑥 ) = {
𝑃𝑟𝑖𝑛𝑡 𝑂ℎ!, 𝑂𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒

Task02 (string with loop)

Modify task01 and do each task with loop. Use strlen(string) with char[]
or use size() or length() with string var function to calculate length
of string.

Task03 (Substring, Casting)

Design a function as void extractInfoAsk(string a) user to input string


and perform following tasks:

a) Input date (28/10/2019) in string and separate day, month and year
in integer type.
b) Input student id (021-12-0006) and tell student’s department and
enrolled year.
c) Input phone number (0300-1234567) and tell the network, user is
using.

Task04 (Concatenation)

Ask user two inputs firstname and lastname and perform following tasks:

a) Concatenate both names and print on screen separated by space.


b) Concatenate both names spaces, separated by “-”.
c) Make any professional email (domain name: siba.edu.pk) from
firstname and lastname.

Task05 (Comparison)

Page 3 of 4
Lab08 – Functions CS121 Fundamentals of Programming Instructor: Saif Hassan

Write a code to check whether two words are similar or not. Use strcmp
function.

Task06 (Sentence)

As user input sentence of string type and perform following operations:

a) Calculate total letters in sentence.


b) Calculate total words in sentence.
c) Calculate numbers, vowels and consonants.
d) Ask user input word and check whether it’s in sentence or not.
e) Count how many word is occurring in it.
f) How many words are starting with “A” in sentence?
g) Ask user two inputs (to_replace, with_replace) and replace
to_replace with with_replace in sentence.

Page 4 of 4

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