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

Fix Breadcrumbs Error And m=1? In Blogger

  Remove Breadcrumbs Errors And ?m=1 Breadcrumb is a small path. It points your location on your site. It also affects the SEO of your website.  Use canonical URL , so: data:blog.url.canonical  instead of data:blog.url. It should work for both desktop and mobile versions. It means the same URL works on both mobile and desktop without producing the ?m=1 or ?m=0. Before this, I am going to tell you what all the portions I will explain and fix your problem. First we will fix Breadcrumbs Error . This is the most dangerous for your website. Second we will remove ?m=1 and ?m=0, tags from our mobile view URL. Remove Breadcrumbs Error :- You should follow the below steps, so that you would fixed this error :- Go to blogger.com Login to blogger with your blogger email id Click on Template Click edit HTML, you have to edit HTML theme code. Press Ctr + F to open a search bar in your HTML theme code. Search for <div class='breadcrumbs' Paste the below code up to </b:loop> ...

Blogger : How To Create Blog Through Blogger & Its Overview

Overview Of Creation Of Blog Now days online learning and earning is very popular. Most of the people is trying to either learn or earn from their home. If you are a writer or story teller, you have interest in writing or reading new thing. So, This is very helpful for you, in this post i will tell you how to create your own blog website and earn money. Step follow to create a blog on blogger:- First you should have a email account and phone number to open or create a blogging account. You have to go to blogger website page, URL of that page is given below, simply you go and click on the URL. Click on the create new blog, after clicking, you have enter the blog name that you want to show to the user and you must choose or set the appropriate your own blog URL. After this process, you will have created your blog. You can handle everything post, pages, layout, inside the setting,you can set the user information and the account information. In the layout you can set layout from the given ...

API : What is an Application Programming Interface

Application Programming Interface API का पूरा नाम "Application Programming Interface" हैं| यह एक software intermediary है जो दो software applications को एक दूसरे से बात करने का कम करता हैं| जैसे कि उद्धरण के लिए Instagram पर आपको message आता है और आप अपने phone पर check कर लेते है API के कारण, क्योंकि API एक माध्यम है आपके फ़ोन में आप किसी भी application से इनफार्मेशन प्राप्त कर पते  हैं| जब आप अपने phone में  कोई  application use करते है तो application internet से जुड़ता है और data को server पर  भेजता है| तब वह server, data को लेता है और translate करता है इस data को , उसके बाद उस data पर जरुरी कार्य करता है और अंतिम में जो translated data है उसे phone पर send करता हैं| तब Application उस data को translate करता है और आपके फ़ोन पर दिखता है readable form में जैसा आप देखना चाहते हैं|  ये जितने भी कार्य है, phone से data को लेना और उसे translate कर के server को send करना और फिर उसे phone पर user के readable form में change करके उसके फ़ोन पर डिस्प्ले करना, ये सब API के मा...