Skip to main content

C Programs : Basic Problems And Its Output

C Basics Programs

1.Check a string is a palindrome or not?

Program:-

#include<stdio.h>

#include<string.h>

void isPalindrome(char *str)

{

int i,l,h;

l=strlen(str);

i=0;

h=l-1;

while(h>i)

{

if(str[i++]!=str[h--])

{

printf("Not Palindrome");

return;

}

}

printf("Palindrome");

}

int main()

{

int i,l,flag=0;

char str[20];

printf("Enter a string:-");

scanf("%s",str);

isPalindrome(str);  

 return 0;

}

Output:-

Enter a string:- abcdcba

Palindrome


2.Find the maximum frquency of a character in a string.

Program:-

#include<stdio.h>

#include<string.h>

//#define ASCII_SIZE 256

char string_freq(char *str)

{

int i,j,l,cmax=0,count;

char s;

l=strlen(str);

for(i=0;i<l;i++)

{

count=1;

for(j=i+1;j<l;j++)

{

if(str[i]==str[j])

{

count++;

}

}

if(cmax<count)

{

cmax=count;

s=str[i];

}

}

printf("\n%d",cmax);

return s;

}

int main()

{

char str[20];

printf("Enter a string:-");

scanf("%s",str);

printf("\n%c",string_freq(str));

return 0;

}


3. Remove the duplicate characters from a string.

Program:-

#include<stdio.h>
#include<string.h>
#define ASCII_SIZE 256
void remove_duplicate(char *str)
{
int count[ASCII_SIZE]={0};
int i,j,len;
len=strlen(str);
char b[len];
j=0;
for(i=0;i<len;i++)
{
count[str[i]]++;
if(count[str[i]]==1)
{
b[j]=str[i];
j++;
}
}
printf("Display string:-");
printf("%s",b);
}
int main()
{
char str[20];
printf("Enter a string:-");
scanf("%s",str);
remove_duplicate(str);
return 0;
}

Output:-
Enter string:- programming
poain

4.Find duplicate of a string?

Program:-

#include<stdio.h>
#include<string.h>
void print_duplicate(char *str)
{
int i,len,count,j;
len=strlen(str);
for(i=0;i<len;i++)
{
count=1;
for(j=i+1;j<len;j++)
{
if(str[i]==str[j])
count++;
}
  if(count>=2)
  {
    printf("%c",str[i]);
    printf("%d\n",count);
  }
    }
}

int main()
{
char str[20];
printf("Enter a string:-");
scanf("%s",str);
print_duplicate(str);
  return 0;
}


5. Remove the characters from the first string which common in the second string.

Program:-

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define ASCII_SIZE 256
 
//Remove characters from string1 which are in string2
char *RemoveChars(char *string1, char *string2)
{
  //Count array stores the count of chars from string2
  int *count = (int *)calloc(sizeof(int), ASCII_SIZE);
  for(int i = 0; *(string2+i);  i++)
  {
      count[*(string2+i)]++;
  }
  int i  = 0, j = 0;
  while(*(string1 + i))
  {
    char temp = *(string1 + i);
    //If count of charcter is zero add to output
    if(count[temp] == 0)
    {
        *(string1 + j) = *(string1 + i);
        j++;
    }
    i++;
  }    
 //Null termination 
  //removing extra characters
  *(string1+j) = '\0';    
 
  return string1;
}
 
int main()
{
char str1[25],str2[20];
printf("Enter the first string:-");
scanf("%s",str1);
printf("\nEnter 2nd string:-");
scanf("%s",str2);
printf("%s",RemoveChars(str1,str2));
return 0;
}


Comments

Popular posts from this blog

Merge Arrays : Merge Two Arrays Of Different Size In C++

Merge Two Arrays C++ Program C++ program to merge two arrays of different size. First array size is m and second array size is n. The size of the first array is greater than the first array i.e. m>n.                               Algorithm :- Enter the size of first array m. Enter the size of 2nd array n. Enter the elements of 1st and 2nd array and number of elements of first array should be greater than the number of elements of second array. After m index of first array, insert the element of second array into the first array up to m+n-1. Sort the merge array of size m+n. Display the first array, which will display all the elements of the merge array of size m+n. Program: - #include<iostream> using namespace std; int main() { int i,n,m,a[15],b[5],j;//array a[] and b[] are declared cout<<"Enter the size 1st array:-"<<endl; cin>>m; cout<<"Enter the size of 2nd array(n<m...

C++ Vector : Basics Of Vector in C++ STL

In C++, Vector is unlike array, it stores different data types such as int, double, string, float, etc. It works like the dynamic array with the ability to resize automatically itself. Vector stores data in the contiguous memory location. Two functions are needed to traverse from starting to end that is begin() and end() functions.                               Syntax of the vector declaration:-             vector<data_type> variable_name (number_of_elements);  Here number_of_elements is optional, we can also declare a vector with empty vector that contains zero elements. The data_type in the angle-brackets indicates any type of data type which is valid in c++. Vector declaration examples:- vector<int> numbers (10); //In this example, we declared a vector name number of 10 integers. vector<string> names;  //In this, a vector name declared of string. In eve...

Data Structure And Algorithm Important Topics Overview

  Overview Of Data Structures And Algorithms This post will give you the clear idea about learning data structures and algorithms . So, Data structure? => In computer science a data structure is a data organization , management and storage format that enables efficient access and modification. Or in the other words it is a way in which data is stored on a computer. Types of data structures:- Array  String Stack Queue Linked List  Binary Tree Binary Search Tree Heap Hash Table Graph Above topics, array is a linear data structure which stores the data in the sequence order, dynamic array, every data is stored in the next contiguous memory location.   String is a collection of characters. If it contains the alphabets and it also uses the digit as character. If a string is composed of numbers and characters then numbers are also treated as characters. Stack is also a linear data structure. It works either on Last In First Out or First In Last Out . It has only one ...