0% found this document useful (0 votes)
3 views15 pages

Programing

The document contains a series of C programming tasks with specified functionalities, including date comparison, sorting arrays, searching for characters in strings, string partitioning, and more. Each task includes a description and the corresponding C code implementation. The code examples demonstrate various programming concepts such as loops, conditionals, and string manipulation.

Uploaded by

ammanayana2000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views15 pages

Programing

The document contains a series of C programming tasks with specified functionalities, including date comparison, sorting arrays, searching for characters in strings, string partitioning, and more. Each task includes a description and the corresponding C code implementation. The code examples demonstrate various programming concepts such as loops, conditionals, and string manipulation.

Uploaded by

ammanayana2000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

C Programing Q and A

1. Statement- compare dates


Description- Your task here is to implement C Programming code based on the
following specifications. Note that your code should match the specifications in a
precise manner. Consider default visibility of Function, data types unless mentioned.

#include <stdio.h>
int main(void) {
// your code goes here
int n;
scanf("%d",&n);
while(n--){
int day1,mon1,year1;
int day2,mon2,year2;
scanf("%d/%d/%d",&day1,&mon1,&year1);
scanf("%d/%d/%d",&day2,&mon2,&year2);
if(year1>year2){
printf("L\n");
}
else if(year1<year2){
printf("E\n");
}
else{
if(mon1>mon2){
printf("L\n");
}
else if(mon1<mon2){
printf("E\n");
}
else{
if(day1>day2){
printf("L\n");
}
else if(day1<day2){
printf("E\n");
}
else{
printf("S\n");
}
}
}
}
return 0;
}
2. Statement-cruel boss
Description- Your task here is to implement C Programming code based on the
following specifications. Note that your code should match the specifications in a
precise manner. Consider default visibility of Function, data types unless mentioned.
#include <stdio.h>
void sort(int *arr,int length){
int temp=0;
for (int i = 0; i < length; i++) {
for (int j = i+1; j < length; j++) {
if(arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
int main()
{
int N;
scanf("%d",&N);
while (N--){
int arrLength,i;
scanf("%d",&arrLength);
int arr[arrLength];
for (i=0;i<arrLength;scanf("%d",&arr[i++]));
sort(arr,arrLength);
int start,end;
start = arr[0];
end=0;
for (i=1;i<arrLength;i++){
if ((arr[i] - arr[i-1]) > 1){
if (end == 0){
printf("%d",start);
}
else{
printf("%d-%d",start,end);
}
start = arr[i];
end=0;
if (i != (arrLength))
{
printf(",");
}

}
else{
end = arr[i];
}
}
if (end==0){
printf("%d",start);
}
else if (end != 0){
printf("%d-%d",start,end);
}
printf("\n");
}
return 0;
}

3. Statement-Search Character and Substring


Description-Your task here is to implement C Programming code based on the
following specifications. Note that your code should match the specifications in a
precise manner. Consider default visibility of Function, data types unless
mentioned

#include<stdio.h>
#include<string.h>
int main()
{
int n,sum;
scanf("%d\n",&n);
for(int i=0;i<n;i++)
{
char str[100];
scanf("%[^\n]%*c",str);
sum=0;
for(int i=0;i<strlen(str);i++)
{
if(str[i]=='a')
sum=sum+i;
}
char *f;
f=strstr(str,"is");
if(f)
sum+=f-str;
if(sum>0)
printf("%d\n",sum);
else
printf("-2\n");
}
}

4. Statement-string partitioning
Descriptions-Your task here is to implement C Programming code based on the
following specifications. Note that your code should match the specifications in a
precise manner. Consider default visibility of Function, data types unless mentioned.

#include <stdio.h>
#include<string.h>
#include<ctype.h>
int findways(char* s){
int n,count=0,part,result=0,i=0,j;
char a[]={'A','E','I','O','U'};
n=strlen(s);
while(s[i])
{
if(toupper(s[i])=='A'||toupper(s[i])=='E'||
toupper(s[i])=='I'||
toupper(s[i])=='O'||toupper(s[i])=='U')

count++;
i++;
}
i=0;
while(s[i])
{
if(toupper(s[i])=='A'||toupper(s[i])=='E'||
toupper(s[i])=='I'||
toupper(s[i])=='O'||toupper(s[i])=='U')
result++;

if(result==((count/2)+1))
{
part=i;
break;
}
i++;

}
return (n-part-1);
}
int main(){
char c[100];
scanf("%s",c);
int b=findways(c);
printf("%d",b);
return 0;
}

5. Statement-string operations
Descriprion-Your task here is to implement C Programming code based on the following
specifications. Note that your code should match the specifications in a precise manner.
Consider default visibility of Function, data types unless mentioned.
#include <stdio.h>
#include <string.h>
int main() {
char str[50];
scanf("%s",str);
int c=0;
int flag=1;
while(flag){
flag=0;
for(int i=0;i<strlen(str)-1;i++){
if(str[i+1]-str[i]==1){
int a=i;
for(int j=i+2;j<strlen(str);j++){
str[a++]=str[j];
}
str[strlen(str)-1]='\0';
str[strlen(str)-1]='\0';
c++;a
flag=1;
}
}
}
printf("%d",c);
return 0;
}

6. Satement-Sort subsrting alphabetically


Description- Your task here is to implement C Programming code based on the following
specifications. Note that your code should match the specifications in a precise manner.
Consider default visibility of Function, data types unless mentioned.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {

char str[100];
scanf("%s",str);
char arr[10][10];
char *token=strtok(str,",");
int a=0;
while(token!=NULL){
strcpy(arr[a],token);
token=strtok(NULL,",");
a++;
}
char temp[20];
for(int i=0;i<a;i++)
for(int j=i+1;j<a;j++){
if(strcmp(arr[i],arr[j])>0){
strcpy(temp,arr[i]);
strcpy(arr[i],arr[j]);
strcpy(arr[j],temp);
}
}
for(int i=0;i<a;i++){
printf("%s",arr[i]);
if(i!=(a-1)){
printf(",");
}
}
return 0;
}

7. Statement- Non anagram


Description- Your task here is to implement C Programming code based on the following
specifications. Note that your code should match the specifications in a precise manner.
Consider default visibility of Function, data types unless mentioned.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {

char str[100];
fgets(str,100,stdin);
char *token=strtok(str,"[");
strcpy(str,strtok(token,"]"));
char **arr=(char **)malloc(10*sizeof(char*));
token=strtok(str,", ");
int a=0;
while(token!=NULL){
arr[a]=(char *)malloc(strlen(token)*sizeof(char));
strcpy(arr[a],token);
token=strtok(NULL,", ");
a++;
}
char **ch=(char **)malloc(a*sizeof(int));
int l=0;
for(int i=0;i<a;i++){
int f=0;
for(int j=0;j<a;j++){
if(i!=j){
int c=0,c1=0;
if(strlen(arr[i])==strlen(arr[j])){
for(int k=0;k<strlen(arr[i]);k++){
c=c+arr[i][k];
}
for(int k=0;k<strlen(arr[j]);k++){
c1=c1+arr[j][k];
}
if(c==c1){
f=1;
}
}
}}
if(f==0){
ch[l]=(char *)malloc(strlen(arr[i])*sizeof(char));
strcpy(ch[l],arr[i]);
l++;
}
}
for(int i=0;i<l;i++){
if(i==0)
printf("[");
printf("%s",ch[i]);
if(i!=(l-1))
printf(", ");
if(i==(l-1))
printf("]");
}

return 0;
} l++;
}
}
for(int i=0;i<l;i++){
if(i==0)
printf("[");
printf("%s",ch[i]);
if(i!=(l-1))
printf(", ");
if(i==(l-1))
printf("]");
}

return 0;
}

8. Statement-Student score
Description- Your task here is to implement C Programming code based on the
following specifications. Note that your code should match the specifications in a
precise manner. Consider default visibility of Function, data types unless mentioned.
#include<stdio.h>
struct student
{
int roll,mark;
}stud[10],t;
void main()
{
int i,j;
for(i=0;i<10;i++)
{
scanf("%d-%d",&stud[i].roll,&stud[i].mark);
}
for(int i=0;i<10;i++){
for(int j=i+1;j<10;j++){
if(stud[i].roll==stud[j].roll){
if(stud[i].mark>stud[j].mark){
stud[j].roll=NULL;
stud[j].mark=NULL;
}
else{
stud[i].roll=NULL;
stud[i].mark=NULL;
}
}
}
}

for(i=0;i<10;i++)
{
for(j=0;j<10-1;j++)
{
if(stud[j].mark<stud[j+1].mark)
{
t=stud[j];
stud[j]=stud[j+1];
stud[j+1]=t;
}
}
}
for(i=0;i<10;i++)
{
if(stud[i].roll!=NULL){
printf("%d-%d\n",stud[i].roll,stud[i].mark);
}
}
}

9. statement- Day of week


Description- Your task here is to implement C Programming code based on the
following specifications. Note that your code should match the specifications in a
precise manner. Consider default visibility of Function, data types unless mentioned.
#include<stdio.h>
int dayofweek (int dd, int mm, int yy)
{
static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
if(mm<3){
yy -= 1;
}
return (yy + yy / 4 - yy / 100+ yy / 400+ t[mm - 1] + dd) % 7;
}
int main()
{
int dd1, mm1, yy1, dd2, mm2, yy2;
scanf("%d %d %d", &yy1, &mm1, &dd1);
scanf("%d %d %d", &yy2, &mm2, &dd2);
char week [7][10] = {{"SUNDAY"}, {"MONDAY"}, {"TUESDAY"}, {"WEDNESDAY"},
{"THURSDAY"}, {"FRIDAY"}, {"SATURDAY"} };
int day1 = dayofweek (dd1, mm1, yy1);
int day2 = dayofweek (dd2, mm2, yy2);
printf("\n%s", week[day1]);
printf("\n%s", week[day2]);
return 0;
}

10. statement-Insert substring in a string


Description-- Your task here is to implement C Programming code based on the following
specifications. Note that your code should match the specifications in a precise manner.
Consider default visibility of Function, data types unless mentioned.
#include<stdio.h>
#include<string.h>
int main()
{
char str[40];
gets(str);
char instr[20];
gets(instr);
int i,index;
scanf("%d",&i);
int a= strlen(str);
int b= strlen(instr);
char newstr[a+b];
index=0;
int j=0;
while(index <=(a+b))
{
if(index==i)
{
for(int k=0;k<b;k++)
{
newstr[index]=instr[k];
index++;
}
}
else{
newstr[index]=str[j];
index++;
j++;
}
}
printf("%s",newstr);
return 0;
}

11. statement- monitor aaray


Description-- Your task here is to implement C Programming code based on the following
specifications. Note that your code should match the specifications in a precise manner.
Consider default visibility of Function, data types unless mentioned.
#include<stdio.h>
int main()
{
int a;
int arr[100];
int temp=0;
scanf("%d",&a);
for(int i=0;i<a;i++)
{
scanf("%d",&arr[i]);
}
for(int i=0;i<a;i++)
{
for(int j=i+1;j<a;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for(int i=0;i<a;i++)
{
if((arr[i]-arr[i-1])==2)
{
printf("%d",i);
}
}
return 0;
}

12. statement-merge sort


Description-
#include<stdio.h>
int main()
{
int l1,l2,ind=0;
scanf("%d\n%d\n",&l1,&l2);
int len=l1+l2;
int a[len];
for(int i=0;i<l1;scanf("%d ",&a[ind++]),i++);
for(int i=0;i<l2;scanf("%d ",&a[ind++]),i++);
for(int i=0;i<ind;i++)
{
for(int j=i+1;j<ind;j++)
{
if(a[i]<a[j])
{
a[i]=a[i]+a[j]-(a[j]=a[i]);
}
}
}
for(int i=0;i<len;printf("%d\n",a[i]),i++);
return 0;

You might also like