Programing
Programing
#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;
}
#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;
}
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;
}
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);
}
}
}