0% found this document useful (0 votes)
3 views6 pages

Pointers

POINTERS

Uploaded by

hesham sakr
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)
3 views6 pages

Pointers

POINTERS

Uploaded by

hesham sakr
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

Working with Strings in C++

EXPERIMENT NO: 4 Week No# 4


111# 1

Part (A): String Manipulation Using Built-in Functions


Objective
 To understand and apply fundamental string operations in C++ using built-in functions.
 To perform string input/output, concatenation, comparison, and length calculation.
 To reinforce the distinction between C-style strings (char arrays) and C++ string objects.

Theoretical Background
Strings in C++ can be represented as C-style strings (arrays of characters ending with a null
character '\0') or as C++ string objects.

Key built-in string operations include:

1. strlen() – Returns the length of a C-style string.


2. strcpy() – Copies one string to another.
3. strcat() – Concatenates two strings.
4. strcmp() – Compares two strings lexicographically.

C++ string objects offer intuitive operations like + for concatenation, .length() for length, and ==
for comparison.

Description

Students will write a C++ program that:

 Reads two strings from the user.


 Computes their lengths.
 Concatenates the strings.
 Compares the strings.
This introduces students to standard string functions and object-oriented string handling.

Algorithm
1. Start.
2. Declare two strings (C-style or string objects).
3. Input the strings from the user.
4. Calculate the lengths using strlen() or .length().
5. Concatenate the strings using strcat() or +.
6. Compare the strings using strcmp() or ==.
7. Display the results.
8. End.

Program
#include <iostream>
#include <cstring> // For C-style string functions
using namespace std;

36
int main() {
char str1[50], str2[50];
cout << "Enter first string: ";
[Link](str1, 50);

cout << "Enter second string: ";


[Link](str2, 50);

cout << "\nLength of first string: " << strlen(str1);


cout << "\nLength of second string: " << strlen(str2);

char concat[100];
strcpy(concat, str1);
strcat(concat, str2);

cout << "\nConcatenated string: " << concat;

if (strcmp(str1, str2) == 0)
cout << "\nStrings are equal.";
else
cout << "\nStrings are not equal.";

return 0;
}
Sample Output
Enter first string: Hello
Enter second string: World

Length of first string: 5


Length of second string: 5
Concatenated string: HelloWorld
Strings are not equal.
Self-Evaluation

 Did I successfully use strlen(), strcpy(), strcat(), and strcmp()?


 Can I differentiate between C-style strings and C++ string objects?
 Was I able to correctly concatenate and compare strings?
 Did I observe correct output for string length and equality?
 Can I explain how each built-in function works internally?

Viva Questions

1. What is a C-style string, and how does it differ from a C++ string object?
2. What is the purpose of the null character '\0' in C-style strings?
3. How does strlen() calculate string length?
4. What is the difference between strcpy() and strcat()?
5. How does strcmp() determine the relationship between two strings?
6. Can you concatenate two string objects using +?
7. Why is [Link]() used instead of cin for string input?
8. Can C-style strings be passed to functions? How?
9. What precautions must be taken to avoid buffer overflow with C-style strings?
10. Why is string manipulation important in real-world programming?

MCQs
1. Which header file contains C-style string functions?
a) <string>
b) <cstring>

37
c) <iostream>
d) <stdio.h>
2. Which function calculates the length of a C-style string?
a) length()
b) strlen()
c) size()
d) strcount()
3. Which function concatenates two C-style strings?
a) strcpy()
b) strcat()
c) strcmp()
d) strlen()
4. strcmp(str1, str2) returns 0 when:
a) str1 is greater than str2
b) str1 is less than str2
c) str1 equals str2
d) str2 is empty
5. Which operator concatenates two C++ string objects?
a) +
b) *
c) %
d) ==
6. What is the null character in C-style strings?
a) /0
b) \0
c) 0
d) null
7. Which function copies one C-style string into another?
a) strcat()
b) strcpy()
c) strcmp()
d) strlen()
8. [Link](str, 50) is used to:
a) Input a single word
b) Input a complete line including spaces
c) Output a string
d) Clear the buffer
9. C-style strings must be terminated with:
a) \n
b) \0
c) EOF
d) NULL
10. The result of "Hello" + "World" in C++ strings:
a) HelloWorld
b) Hello World
c) Error
d) Hello+World

38
Part (B): Working with Strings Using Pointers and User-Defined Functions
Objective
 To manipulate strings using pointers and modular programming with functions.
 To implement advanced string operations: counting vowels, reversing, and copying.
 To reinforce pointer arithmetic and memory management in C++.

Theoretical Background

Pointers allow direct memory access, which is particularly useful for character arrays. Combining
pointers with user-defined functions enables:

 Efficient traversal of strings.


 Reusable modular functions for common operations.
 Memory-efficient string manipulations.

Key operations include:

 Pointer traversal – Iterating over characters using pointer arithmetic.


 Vowel counting – Identifying and counting vowels in a string.
 String reversal – Swapping characters using pointers.

Description
Students will:

 Input a string from the user.


 Count vowels using a dedicated function.
 Reverse the string using another function.
 Observe pointer-based string manipulation.

This emphasizes modularity and efficient string handling.

Algorithm
1. Start.
2. Declare a character array and pointer.
3. Input the string.
4. Pass the string to a function to count vowels.
5. Pass the string to another function to reverse it.
6. Display vowel count and reversed string.
7. End.

Program
#include <iostream>
using namespace std;

int countVowels(char *p) {


int count = 0;
while (*p != '\0') {
char c = tolower(*p);
if (c=='a'||c=='e'||c=='i'||c=='o'||c=='u') count++;
p++;
}
return count;
39
}

void reverseString(char *s, char *rev) {


int len = 0;
while (s[len] != '\0') len++;

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


rev[i] = s[len - 1 - i];

rev[len] = '\0';
}

int main() {
char str[100], rev[100];
cout << "Enter a string: ";
[Link](str, 100);

cout << "Number of vowels: " << countVowels(str) << endl;


reverseString(str, rev);
cout << "Reversed string: " << rev << endl;

return 0;
}
Sample Output
Enter a string: OpenAI
Number of vowels: 3
Reversed string: IAnepO
Self-Evaluation
 Did I correctly implement pointer-based string traversal?
 Was I able to write modular functions for vowel counting and string reversal?
 Can I explain how pointer arithmetic accesses each character in the string?
 Did the program produce correct vowel count and reversed string output?
 Can I generalize these techniques for other string operations?

Viva Questions
1. How does a pointer traverse a string?
2. Why are user-defined functions useful in string manipulation?
3. How is vowel counting implemented using a pointer?
4. Explain the logic of reversing a string using arrays and pointers.
5. Can pointers be used with C++ string objects?
6. What is the difference between passing by value and passing by pointer?
7. How does the null character affect string traversal?
8. Why is modular programming recommended for string operations?
9. How does the tolower() function assist in vowel counting?
10. What precautions are needed to avoid memory issues with pointer-based strings?

MCQs

1. What does char *p represent?


a) A string object
b) A pointer to a character
c) An integer
d) A float
2. How do you traverse a string using a pointer?
a) Using an index only
b) Using while(*p != '\0')
40
c) Using for(int i=0; i<100; i++)
d) Using strlen()
3. Which function converts a character to lowercase?
a) tolower()
b) toupper()
c) tochar()
d) lower()
4. What does rev[len] = '\0' do in string reversal?
a) Initializes the array
b) Terminates the string
c) Reverses the string
d) Counts the length
5. How are vowels counted in the given program?
a) Using arrays
b) Using pointer traversal and comparison
c) Using recursion
d) Using strcat()
6. Why are pointers preferred in string manipulation?
a) Faster access to memory
b) Slower execution
c) To save characters
d) To avoid loops
7. What is the role of a user-defined function here?
a) Reduce program size
b) Modularize string operations
c) Avoid memory
d) Replace pointers
8. Which of the following is NOT a pointer operation in the program?
a) *p
b) p++
c) rev[i]
d) *(p+i)
9. How is the original string preserved during reversal?
a) By using a separate array
b) By using pointer arithmetic
c) By copying pointer
d) By overwriting
10. What will happen if '\0' is missing in reversed string?
a) Program works fine
b) Output may contain garbage
c) Compiler error
d) None

41

You might also like