0% found this document useful (0 votes)
20 views4 pages

Lab - Report9 2

This document contains instructions for 3 tasks related to C++ strings, pointers, and file input/output. Task 1 involves defining a struct containing an integer and string, declaring an array of this struct, reading 5 integer and string pairs from the user, and printing them. Task 2 explores various string functions like strcpy(), strcat(), strncat(), strlen(), and strcmp() by uncommenting code lines one at a time. Task 3 writes a program to open a passwords.txt file, read an ID and password string on each line using fscanf(), and display the results. The file should be in the same directory as the program.

Uploaded by

barandikmen2004
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)
20 views4 pages

Lab - Report9 2

This document contains instructions for 3 tasks related to C++ strings, pointers, and file input/output. Task 1 involves defining a struct containing an integer and string, declaring an array of this struct, reading 5 integer and string pairs from the user, and printing them. Task 2 explores various string functions like strcpy(), strcat(), strncat(), strlen(), and strcmp() by uncommenting code lines one at a time. Task 3 writes a program to open a passwords.txt file, read an ID and password string on each line using fscanf(), and display the results. The file should be in the same directory as the program.

Uploaded by

barandikmen2004
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/ 4

CMPE223

ALGORITHMS AND PROGRAMMING


2023 – 2024 Fall
LabWork 9: Strings, Pointers, and Files

Name: Abdullah Baran DİKMEN


Student Number: 22211334

Task 1:
Write a complete C++ program that scans 5 integers and 5 strings, stores them in the
structure “struct q2”, and prints them to the screen.

struct q2{
int age;
char name[20];
};

Write a display function to print the contents.

1.1 How would you declare an array of q2 objects to store 5 sets of integers and strings?
struct to define a composite data type containing an integer and a string

1.2 How would you read and store the integer age input by the user?
use the scanf() function
1.3 How would you read and store the string name input by the user?
use the scanf() function with the %s format specifier.
1.4 How would you handle any potential buffer overflow when reading the string name?
use fgets() instead of scanf(). Using fgets() allows you to specify the maximum number of
characters to read,

Task 2: Run the following code ready for string functions. Use one function at a time and
observe the output.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
char a[20]="computer", b[20]="prog";
int x;
printf("%s\t%s\n\n",a,b);
strcpy(a,b);
//strcpy(b,a);
//strncpy(a,b,2);
//strcat(a,b);
//strcat(b,a);
//strncat(a,b,3);
//x = strlen(a);
x = strcmp(b,a);
printf("\nx = %d\n",x);
printf("%s\t%s\n\n",a,b);
system("PAUSE");
return 0;
}

2.1 What is the purpose of the strcpy() function in this code?


Aim of the strcpy()is for the copying the content of string b into string a.
2.2 What would be the result if we uncommented the line //strcpy(b,a);? Explain.
If we uncommented the line //strcpy(b,a);, it will copy the content of string a into the string b.
Respectively, both strings a and b will include "computer."
2.3 If we uncommented the line //strncpy(a, b, 2);, what would be the value of string a after
executing that line?
If we uncommented the line //strncpy(a, b, 2);, the value of string a just after executing this
line gonna be "pr" because the strncpy() function copies the specified number of characters
from the source (b) to (a).So that , it would copy 2 characters from b to a.
2.4 Describe the functionality of the strcat() function. What would be the result if we
uncommented the line //strcat(a,b);?
The strcat() function is used to concatenate or append the content of the source string to the
destination string. If we uncommented the line //strcat(a,b);, the content of the source string b
would be appended to the destination string a.
2.5 Explain what happens when we use the strncat() function. If we uncommented the line
//strncat(a,b,3); , what would be the value of string a after executing that line?
The strncat() function is similar to strcat(), but it allows specifying the number of characters
to concatenate from the source string to the destination string. If we uncommented the line
//strncat(a,b,3);, the value of string a after executing that line would be "computerpro"
because it would add the first 3 characters of string b to string a.
2.6 What is the purpose of the strlen() function? What would be the value of x if we
uncommented the line //x = strlen(a);?
The purpose of the strlen() function is to determine the length of a string, i.e., the number of
characters in the string excluding the null-terminating character. If we uncommented the
line //x = strlen(a);, the value of x would be the length of string a.

Task 3: Write a complete C/C++ program that gets an id and a string from the file
passwords.txt, then displays everything to the screen.

Passwords.txt:
1 asdpe%%2
2 jrpd&*
3 ………..
……………..
3.1 How does the program open the file? What mode is used? By fopen
3.2 How does the program read the content from the file? By fscanf
3.3 where should the “passwords.txt” file be located in order to access the file successfully?

file should be located at the specific absolute path specified in the code

Task 1
#include<iostream>
using namespace std;

struct q2{
int age;
char name[20];

};

void display(q2& data){


cout<<"Age "<<data.age<<endl;
cout<<"Name "<<data.name<<endl;
cout<<endl;

int main(){
q2 data[5];
cout<<"Enter 5 Ages and 5 Names";
for(int i=0;i<5;i++){
cout<<"Age "<<i+1<<": ";
cin>>data[i].age;
cin.ignore();
cout<<"Name "<<i+1<< " :";
cin>>data[i].name;

}
cout<<" Displaying the contents " <<endl;
for(int i=0;i<5;i++){
cout<<i+1<<" : "<<endl;
display(data[i]);

}
return 0;

}
Task 3
#include<iostream>
using namespace std;

int main(){
FILE *file=fopen("password.txt", "r");

if(file==NULL){
printf("Failed to open the file. \n");
return 1;

int id;
char password[100];

while(fscanf(file, "%d %[^\n]", &id, password) !=EOF){

printf("ID : %d, Password : %s\n", id, password);

fclose(file);
return 0;
}

You might also like