Programming Practice in C
Programming Practice in C
Output:
Enter two string rama
lakshmi
Concatenation of two string is ramalakshmi
6) Check two strings are same
#include <stdio.h>
#include<string.h>
int main() {
char str[30], str1[30];
printf("Enter two string ");
scanf("%s%s",&str,&str1);
int flag=1,i=0;
while(str[i]!='\0' && str1[i]!='\0'){
if(str[i]!=str1[i]){
flag=0;
break;
}
i++;
}
if(str[i]!='\0' || str1[i]!='\0'){
flag=0;
}
if(flag){
printf("The two strings are same");
}
else{
printf("The two strings are not same");
}
return 0;
}
Output:
Enter two string ram
ram
The two strings are same
7) Reverse the string
#include <stdio.h>
#include<string.h>
int main() {
char str[30];
printf("Enter two string ");
scanf("%s",&str);
int start=0,end=strlen(str)-1;
while(start < end){
char temp=str[start];
str[start]=str[end];
str[end]=temp;
start++;
end--;
}
printf("The reverse of the string is %s",str);
return 0;
}
Output:
Enter two string ramalakshmi
The reverse of the string is imhskalamar
8)Remove the spaces in the string
#include <stdio.h>
int main() {
char str[50],str1[50];
int j=0;
printf("enter a string: ");
scanf("%[^\n]s",&str);
for(int i=0;str[i]!='\0';i++){
if(str[i]!=' '){
str1[j]=str[i];
j++;
}
}
str1[j]='\0';
printf("String after removing the spaces %s",str1);
return 0;
}
Output:
enter a string: hi hellohi hello
String after removing the spaces hihello
9)Count number of words in the sentence
#include <stdio.h>
int main() {
char str[50];
int count=0;
printf("enter a string: ");
scanf("%[^\n]s",&str);
for(int i=0;str[i]!='\0';i++){
if(str[i]==' '){
count++;
}
}
printf("The number of words in the string is %d",count+1);
return 0;
}
Output:
enter a string: Hi hello welcome to the great karikalan magic show
The number of words in the string is 9
10)Find the first non repeating character in the string
#include <stdio.h>
int main() {
char str[50];
int count,flag=0,i,j;
printf("enter a string: ");
scanf("%[^\n]s",&str);
for(i=0;str[i]!='\0';i++){
count=0;
for(j=0;str[j]!='\0';j++){
if(str[i]==str[j]){
count++;
}
}
if(count==1){
printf("The first non repeting charector in the string is %c",str[i]);
flag=1;
break;
}
}
if(flag==0){
printf("There is no first non repeating charecter in the string");
}
return 0;
}
Output:
enter a string: ramalakshmi
The first non repeting charector in the string is r
11) Find the substring present in the string or not
#include <stdio.h>
int main() {
char str[100],sub[50];
int i,j,found=0;
printf("Enter the string and substring ");
scanf("%s %s",&str,&sub);
for(i=0;str[i]!='\0';i++){
j=0;
while(str[i+j]!='\0' && sub[j]!='\0' && str[i+j]==sub[j]){
j++;
}
if(sub[j]=='\0'){
found=1;
break;
}
}
if(found){
printf("The substring is present in the string");
}
else{
printf("The substring is not present in the string");
}
return 0;
}
Output:
Enter the string and substring ramalakshmi hmi
The substring is present in the string
12)Remove the duplicate characters from the string
#include <stdio.h>
int main() {
char str[100],sub[100];
int i,j,k=0;
printf("Enter the string ");
scanf("%s",&str);
for(i=0;str[i]!='\0';i++){
isDup=0;
for(j=0;j<k;j++){
if(str[i]==str[j]){
isDup=1;
break;
}
}
if(!isDup){
sub[k]=str[i];
k++;
}
}
sub[k]='\0';
printf("The string after removing the duplicates is %s",sub);
return 0;
}
Output:
Enter the string ramalakshmi
The string after removing the duplicates is ramlkshi
Linked List
Traverse a linked list
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void printList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = (struct Node*)malloc(sizeof(struct Node));
struct Node* second = (struct Node*)malloc(sizeof(struct Node));
struct Node* third = (struct Node*)malloc(sizeof(struct Node));
head->data = 10;
head->next = second;
second->data = 20;
second->next = third;
third->data = 30;
third->next = NULL;
printList(head);
free(head);
free(second);
free(third);
return 0;
}
Output:
10 -> 20 -> 30 -> NULL
Insert at the beginning in the linked list
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* insertAtBeginning(struct Node* head, int newData) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = newData;
newNode->next = head;
return newNode;
}
void printList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
head = insertAtBeginning(head, 30);
head = insertAtBeginning(head, 20);
head = insertAtBeginning(head, 10);
printList(head);
return 0;
}
Output:
10 -> 20 -> 30 -> NULL
1. REMOVE DUPLICATES IN AN ARRAY
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cout << "enter the size of array ";
cin >> n;
vector<int> arr(n);
unordered_set<int> seen;
for(int &num:arr){
cin >> num;
}
for(int num:arr){
if(seen.insert(num).second){
cout << num << " ";
}
}
return 0;
}
2. FIND Kth LARGEST AND Kth SMALLEST ELEMENT IN ARRAY
#include <bits/stdc++.h>
using namespace std;
int main() {
int n,k;
cout << "enter the size of array ";
cin >> n;
vector<int> arr(n);
for(int &num:arr){
cin >> num;
}
cout << "Enter the value of k ";
cin >> k;
sort(arr.begin(),arr.end());
for(int num:arr){
cout << num;
}
cout << endl;
cout << "Kth largest element is " << arr[n-k] << endl;
cout << "Kth smallest element is " << arr[k-1];
return 0;
}
3. Move all Zeroes to the End of the Array
#include <bits/stdc++.h>
using namespace std;
int main() {
int n,pos=0;
cout << "enter the size of array ";
cin >> n;
vector<int> arr(n);
for(int &num:arr){
cin >> num;
}
for(int i=0;i<n;i++){
if(arr[i]!=0){
swap(arr[i], arr[pos]);
pos++;
}
}
for(int n2:arr){
cout << n2;
}
return 0;
}
4. Rotate Array by N Elements
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[6]={2,3,4,5,6,7};
int n=1;
int size=sizeof(arr)/sizeof(arr[0]);
rotate(arr, arr+n, arr+size);
for(int num:arr){
cout << num << " ";
}
return 0;
}
5. Sort the array in reverse order
#include <bits/stdc++.h>
using namespace std;
int main() {
vector <int> arr;
arr={2,3,4,5,6,7};
int size=arr.size();
sort(arr.begin(), arr.end());
reverse(arr.begin(), arr.end());
for(int num:arr){
cout << num <<" ";
}
}
6. Max count of zero and one
#include <bits/stdc++.h>
using namespace std;
void maxone(vector<int> & arr, int size){
int maxcount=0, currentcount=0,i;
for(i=0;i<size;i++){
if(arr[i]==1){
currentcount++;
maxcount=max(maxcount,currentcount);
}
else{
currentcount=0;
}
}
cout << "max one " << maxcount;
}
void maxzero(vector<int> & arr, int size){
int maxcount=0, currentcount=0,i;
for(i=0;i<size;i++){
if(arr[i]==0){
currentcount++;
maxcount=max(maxcount,currentcount);
}
else{
currentcount=0;
}
}
cout << "max zero " << maxcount;
}
int main() {
vector <int> arr;
arr={1,1,1,1,0,0,1,0,0,0};
int size=arr.size();
maxzero(arr, size);
maxone(arr, size);
}
7. Sort array in wave form
#include <bits/stdc++.h>
using namespace std;
int main() {
vector <int> arr;
arr={3,5,2,6,1,7,8,9};
int size=arr.size();
sort(arr.begin(), arr.end());
for(int num:arr){
cout << num <<" ";
}
cout << endl;
for(int i=0;i<size;i+=2){
int temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
for(int num:arr){
cout << num <<" ";
}
return 0;
}
8. Pattern prints
#include <bits/stdc++.h>
using namespace std;
int main() {
int n,i,j;
cin >> n ;
char ch='A';
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
cout << ch <<" ";
}
ch++;
cout << endl;
}
return 0;
}
2
#include <bits/stdc++.h>
using namespace std;
int main() {
int n,i,j,num=1;
cin >> n ;
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
cout << num <<" ";
num++;
}
cout << endl;
}
return 0;
}
3. #include <bits/stdc++.h>
using namespace std;
int main() {
int n,i,j,num=1;
cin >> n ;
for(i=1;i<=n;i++){
for(j=1;j<=n-i;j++){
cout <<" ";
}
int num=i;
for(j=1;j<=i;j++){
cout << num<<" ";
num++;
}
num-=2;
for(j=1;j<i;j++){
cout << num << " ";
num--;
}
cout << endl;
}
return 0;
}
4) #include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
cout << " ";
}
for (int j = 0; j < (2 * (n - i) - 1); j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
5 #include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
cout << " ";
}
for (int j = 0; j < n; j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
6 #include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 2 * i; j++) {
cout << " ";
}
for (int j = 0; j < n - i; j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
7 #include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || i == n - 1 || j == 0 || j == n - 1)
cout << "* ";
else
cout << " ";
}
cout << endl;
}
return 0;
}
Linked list
1. Insert and display1
#include <bits/stdc++.h>
using namespace std;
struct node{
int data;
node* next;
};
void insert(node*& head, int data){
node* newnode=new node();
newnode->data=data;
newnode->next=NULL;
if(head==NULL){
head=newnode;
return;
}
node *temp=head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=newnode;
}
void display(node * head){
node * temp=head;
while(temp!=NULL){
cout << temp->data << "->";
temp=temp->next;
}
}
int main() {
node * head=NULL;
insert(head,10);
insert(head,20);
insert(head,30);
insert(head,40);
cout << "The linked list elements are ";
display(head);
return 0;
}
Output:
The linked list elements are 10->20->30->40->
2. Insert at beginning
#include <bits/stdc++.h>
using namespace std;
struct node{
int data;
node* next;
};
void insertBegin(node*& head, int data){
node* newnode=new node();
newnode->data=data;
newnode->next=head;
head=newnode;
}
void display(node * head){
node * temp=head;
while(temp!=NULL){
cout << temp->data << "->";
temp=temp->next;
}
}
int main() {
node * head=NULL;
insertBegin(head,10);
insertBegin(head,20);
insertBegin(head,30);
insertBegin(head,40);
cout << "The linked list elements are ";
display(head);
return 0;
}
Output:
The linked list elements are 40->30->20->10->