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

Lab 6

The document provides solutions to 7 programming tasks involving C-string functions and operations. Task 1 has programs to demonstrate the strlen, strcat, strcpy, and strcmp string functions. Tasks 2-4 involve writing functions to count characters or words in a string. Task 5 reverses a string. Task 6 sums the digits in a string. Task 7 finds the most frequently occurring character in a string.

Uploaded by

Khawar Khalil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Lab 6

The document provides solutions to 7 programming tasks involving C-string functions and operations. Task 1 has programs to demonstrate the strlen, strcat, strcpy, and strcmp string functions. Tasks 2-4 involve writing functions to count characters or words in a string. Task 5 reverses a string. Task 6 sums the digits in a string. Task 7 finds the most frequently occurring character in a string.

Uploaded by

Khawar Khalil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Page 1 of 7

TASK #1:
Write a program to observe the behavior of following string functions:
a) strlen b) strcat c) strcpy d) strcmp
SOLUTION:
a) strlen
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char name[]="KHAWAR KHALIL";
int length;
length=strlen(name);
cout<<length;
}
---------------------------------------------------------------------------------------------------------------------
b) strcat
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
const int size=10;
char string1[size]="Hello";
char string2[size]="World";
cout<<string1<<endl;
cout<<string2<<endl;
strcat(string1,string2);
cout<<string1 <<endl;
}
---------------------------------------------------------------------------------------------------------------------
c) strcpy
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
const int size=10;
char string1[size]="Hello";
char string2[size]="World";
cout<<string1<<endl;
cout<<string2<<endl;
strcpy(string1,string2);
cout<<string1 <<endl;
cout<<string2<<endl;
}
Page 2 of 7

d) Strcmp
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
const int size=100;
char string1[size],string2[size];
cout<<"Enter 1st string: ";
cin.getline(string1,size);
cout<<"Enter 2nd string: ";
cin.getline(string2,size);
if(strcmp(string1,string2)==0)
cout<<"string are same" <<endl;
else
cout<<"string are not same"<<endl;
}
Task #2:
Write a program that returns an integer and accepts C-string as an argument. The program
should count the number of characters in the string and return that number.
SOLUTION:
#include <iostream>
using namespace std;
int Char_Counter(char *Pointer)
{
int counted = 0;
while(*Pointer != '\0')
{
counted++;
Pointer++;
}
return counted;
}

int main()
{
char C_String[1000];
cout << "Enter a string: ";
cin.getline(C_String, 1000);
cout << "You entered characters: ";
cout << Char_Counter(C_String);
}

TASK #3:
Write a program that accepts C-string as an argument and return the number of words
contained in the string. e.g: “we are together” the program should display the number 3.
Page 3 of 7

SOLUTION:
#include <iostream>
using namespace std;
int Char_Counter(char *Pointer)
{
int counted = 1;
while(*Pointer != '\0')
{
if (*Pointer == ' ')
counted++;
Pointer++;
}
return counted;
}

int main()
{
char C_String[1000];
cout << "Enter a string: ";
cin.getline(C_String, 1000);
cout<<"You entered Words in string: ";
cout<<Char_Counter(C_String);
}

TASK #4:
Modify the program 3, so it also displays the number of letters in each word.
SOLUTION:
#include <iostream>
using namespace std;
int Word_Counter(char *Pointer)
{
int counted = 1;
while(*Pointer != '\0')
{

if (*Pointer == ' ')


counted++;
Pointer++;
}
return counted;
}

int Char_Counter(char *Pointer)


{
int counted = 0;
while(*Pointer != '\0')
{
counted++;
Pointer++;
Page 4 of 7

}
return counted;
}

int main()
{
char C_String[1000];
cout << "Enter a string: ";
cin.getline(C_String, 1000);
cout << "\n**** You entered " <<Word_Counter(C_String)
<<" Words in that string ****\n"
<< Word_Counter(C_String) <<" Words having "
<< Char_Counter(C_String)
<< " characters with spaces.\n\t\tand\n"
<< Word_Counter(C_String) <<" Words having "
<< (Char_Counter(C_String)) - (Word_Counter(C_String))
<< " characters without spaces.\n";
}
Page 5 of 7

TASK #5:
Write a program that accepts C-string as an argument and display reverse order of string.
SOLUTION:
#include<iostream>
#include<string.h>
using namespace std;
int main ()
{
char str[50], temp;
int i, j;
cout << "Enter a string : ";
gets(str);
j = strlen(str) - 1;
for (i = 0; i < j; i++,j--)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
cout << "\nReverse string : "
<< str;
return 0;
}
TASK #6:
Write a program that ask user to enter a series of single digit number with
nothing separating them. Read the input as a string. The program should display the
sum of all the single digit number in the string. e.g: 2514 →12.
SOLUTION:
#include<iostream>
#include<string.h>
//conversion char to int
#define charToInt(c) (c-'0')
using namespace std;
int main()
{
int sum = 0;
string str = " ";
cout << "Enter Single digit: "; cin>>str;
for (int i=0; i<str.length(); ++i)
{
//convert charater to int
int a = charToInt(str[i]);
sum = sum + a;
}
//Display Sum
cout<<"\nSum of all digits: "<<sum<<endl;
}
Page 6 of 7

TASK #7:
Write a function that accepts C-string as an argument. The program should return the
character that appears most frequently in the string.
SOLUTION:
#include <iostream>
#include <cstring>
using namespace std;
void Frequently(char *ptr){
int char_count, F_count = 0;
char Freq_Occur;
for(int counter = 0; counter < strlen(ptr); counter++){
char_count = 0;
for(int index = counter; index < strlen(ptr); index++){
if(ptr[counter] == ptr[index])
char_count++;
if(char_count > F_count){
F_count = char_count;
Freq_Occur = ptr[counter];
}
}
}
cout << "Most frequently occurring character: " << Freq_Occur;
cout << "' with " << F_count << " occurrences.";
}

int main()
{
char inputString[1000];
cout << "Enter a string: ";
cin.getline(inputString, 1000);
Frequently(inputString);
}
Page 7 of 7

Prepared By: Khawar Khalil

You might also like