ROLL NO: - 21BCH064
COURSE CODE: - 2CS101
SUBJECT: - COMPUTER PROGRAMMING
PRACTICAL NO: - 6
6A. Write a program
AIM: To delete a character entered by the user from the input string. All
occurrences of the input character should be deleted from the string.
Methodology:
#include<stdio.h>
#include<string.h>
void main()
{
char arr[30];
char brr[30];
int j = 0;
printf("PLEASE ENTER THE STRING:\n");
gets(arr);
printf("PLEASE ENTER THE CHARACTER TO DELETE:\n");
char b;
scanf("%c", &b);
for(int i = 0; i < strlen(arr); i++){
if(arr[i] == b) continue;
else {brr[j] = arr[i]; j++;}
}
puts(brr);
}
Theoretical Principles used: We learnt to delete a character from a string
Input: string entered was “pratham” and the character to be deleted was ‘a’
ROLL NO: - 21BCH064
COURSE CODE: - 2CS101
SUBJECT: - COMPUTER PROGRAMMING
PRACTICAL NO: - 6
Output:
6B.
AIM: Write a program to swap even positioned characters with odd positioned
characters in a given string.
Methodology:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[20],tmp;
int i,j;
gets(str);
printf("ENTER A STRING:");
scanf("%s",str);
ROLL NO: - 21BCH064
COURSE CODE: - 2CS101
SUBJECT: - COMPUTER PROGRAMMING
PRACTICAL NO: - 6
printf("\nTHE ORIGINAL STRING:%s",str);
for(i=0;i<strlen(str);i=i+2)
{
tmp=str[i];
str[i]= str[i+1];
str[i+1]=tmp;
i=i+2;
}
printf("\nAFTER SWAP, THE STRING:%s",str);
puts(str);
}
Theoretical principles used: learned the function of strlen,
Input: Yug
Output:
6C.
ROLL NO: - 21BCH064
COURSE CODE: - 2CS101
SUBJECT: - COMPUTER PROGRAMMING
PRACTICAL NO: - 6
AIM: Read a name from key the board and find out how many times the
character (case insensitive) is repeated.
Methodology:
#include<stdio.h>
#include<string.h>
void main()
{
char arr[30];
int brr[26];
for(int i=0;i<26;i++) brr[i]=0;
printf("PLEASE ENTER THE STRING:\n");
gets(arr);
for(int i=0;i<strlen(arr);i++)
{
int d=arr[i];
if(d<91)
{
brr[d-65]++;
}
else
{
brr[d-97]++;
}
}
int k=0;
ROLL NO: - 21BCH064
COURSE CODE: - 2CS101
SUBJECT: - COMPUTER PROGRAMMING
PRACTICAL NO: - 6
for(int i=0;i<26;i++)
{
if(brr[i]>1)k++;
}
if(k==0)
printf("NONE OF THE CHARACTERS ARE REPEATING!");
else
{
for(int i=0;i<26;i++)
if(brr[i]>1)
printf("%c IS REPEATING %d TIMES!\n",i+97,brr[i]);
}
}
Theoretical principles used: we can see that a character is being repeated for
how many times using various string functions like strlen
Input: Pratham
Output:
ROLL NO: - 21BCH064
COURSE CODE: - 2CS101
SUBJECT: - COMPUTER PROGRAMMING
PRACTICAL NO: - 6
6D.
AIM: Write a program to sort the strings entered by the user as per dictionary
order.
Methodology:
#include<stdio.h>
#include<string.h>
int main()
{
char str[5][50],temp[50];
printf("ENTER ANY 5 STRINGS:");
// Getting strings input
for(int i=0;i<5;++i)
{
fgets(str[i], sizeof(str[i]), stdin);
}
ROLL NO: - 21BCH064
COURSE CODE: - 2CS101
SUBJECT: - COMPUTER PROGRAMMING
PRACTICAL NO: - 6
// storing strings in the lexicographical order
for(int i=0;i<5;++i)
{
for(int j=i+1;j<5;++j)
{
// swapping strings if they are not in the lexicographical order
if (strcmp(str[i],str[j])>0)
{
strcpy(temp, str[i]);
strcpy(str[i], str[j]);
strcpy(str[j], temp);
}
}
}
printf("\nIN THE LEXICOGRAPHICAL ORDER:\n");
for (int i=0;i<5;++i)
{
fputs(str[i], stdout);
}
return 0;
}
Theoretical principles used:
Output:
ROLL NO: - 21BCH064
COURSE CODE: - 2CS101
SUBJECT: - COMPUTER PROGRAMMING
PRACTICAL NO: - 6