agmegharaj
BAN USERThis is my solution,
1)find the max value, create and array of that size and make each value present to one.
2) now find the sequence of 1's and find the rage of the indexes.
3) print the output.
Here is the below code.
#include<stdio.h>
#include<malloc.h>
int main()
{
int array[9] = {4,5,1,5,7,6,8,4,1},i,max = array[0],maxindex,minindex,count = 0,maxcount = 0;
/*finding the max*/
for(i = 0; i<9;i++) {
if(array[i] > max)
max = array[i];
}
int *array1 = (int *)calloc(max,sizeof(int));
for(i = 0; i<9 ; i++)
array1[array[i]] = 1;
for(i=0; i<9; i++) {/*find the range index*/
count = 1;
if(array1[i] == 1) {
while(array1[i] == array1[i+1]) {
count++;
i++;
}
if(count > maxcount) {
maxcount = count ;
minindex = i - count;
maxindex = i - 1;
}
}
}
for(i=minindex; i<=maxindex; i++) /* print the outpupt*/
printf("output : array[%d] = %d\n",i,array[i]);
printf("count = %d\n",count);
printf("maxindex = %d minindex = %d\n",maxindex,minindex);
free(array1);
return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
char c[] = "today is the day",*ptr = c;
int a[256] = {0},count,strcount = 0,i;
for(i=0;i<strlen(c);i++) {
a[c[i]]++;
if(a[c[i]] == 1) {
c[strcount] = c[i];
strcount++;
}
}
c[strcount] = '\0';
printf("string = %s\n",c);
return 0;
}
#include<stdio.h>
int main()
{
int array[16] = {10,13,1,2,3,7,4,5,6,7,8,9,10,9,1,13},*ptr = array,count = 0,maxcount = 0,i,value = 0,index = 0;
for(i =0 ; i<16;i++) {
count = 0;
if((array[i] +1) == array[i+1]) {
index = i;
while((array[i] + 1) == array[i+1]) {
count++;
i++;
}
if(count > maxcount) {
maxcount = count;
value = index;
}
}
}
for(i = value;i<=value +maxcount;i++)
printf("value = %d\n",array[i]);
return 0;
}
I think he's right since in the question, if observed carefully they have mentioned " array with sequence of numbers"
- agmegharaj November 29, 2013this my working code, written in c
#include<stdio.h>
#include<string.h>
int main()
{
char c[] = "abba",*ptr = c,*c1,*c2;
int count = strlen(c);
while(*ptr != '\0') {
if(*ptr == *(ptr+1)) {
count = count +1;;
c1 = ptr - 1;
c2 = ptr + 2;
while(*c1 == *c2) {
count++;
c1++;
c2++;
}
}
if(*ptr == *(ptr+2)) {
count++;
c1 = ptr -1;
c2 = ptr + 3;
while(*c1 == *c2) {
count++;
c1++;
c2++;
}
}
ptr++;
}
printf("number of possible palindrome is the string = %d\n",count);
return 0;
}
This is my solution. I have checked it. It works. Please do find the code below its written in c.
#include<stdio.h>
#include<string.h>
int main()
{
char c[] = "abcicbbcdefggfed",*ptr = c,*c1,*c2,*prev,*str;
int count,maxcount = 0;
while(*ptr != '\0') {
count = 0;
if(*ptr == *(ptr+1)) {
count = 2;
c1 = ptr - 1;
c2 = ptr + 2;
while(*c1 == *c2) {
count = count +2;
prev = c1;
c1--;
c2++;
}
if(count > maxcount) {
maxcount = count;
str = prev;
}
}
ptr++;
}
printf("\n");/*lets print the biggest even palindrome*/
while(maxcount != 0) {
printf("%c",*str);
str++;
maxcount--;
}
printf("\n");
return 0;
}
I think I have the best solution, please see the code below.
int main()
{
char c[] = "this is test";
int i,j;
for(i=strlen(c)-1;i>=0;i--) {
if((c[i] == ' ') || (i == 0)) {
j = (i == 0) ? i : i+1;
for(;j<strlen(c) && (c[j] != ' ');j++)
printf("%c",c[j]);
printf(" ");
}
}
return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
char c[] = {'r','g','b','g','b','g','b','r','r','b','g','r','r','b','b','g','r'},temp;
int r=0,g=0,b=0,i;
for(i=0;i<(sizeof(c)/sizeof(c[0]));i++) {
if(c[i] == 'r') {
temp = c[r];
c[r] = c[i];
c[i] = temp;
r++;
g++;
b++;
}
else if(c[i] == 'g') {
temp = c[g];
c[g] = c[i];
c[i] = temp;
g++;
b++;
}
else if(c[i] == 'b') {
temp = c[b];
c[b] = c[i];
c[i] = temp;
b++;
}
}
for(i=0;i<(sizeof(c)/sizeof(c[0]));i++)
printf("%c",c[i]);
printf("\n");
return 0;
}
#include<stdio.h>
#include<malloc.h>
struct node {
int data;
struct node *link;
};
int append(struct node **q,int num) {
struct node *temp, *r;
temp = *q;
if(temp == NULL) {
temp = (struct node *) malloc(sizeof(struct node));
temp->data = num;
temp->link = NULL;
*q = temp;
return 0;
}
while(temp->link != NULL)
temp = temp->link;
r = (struct node *) malloc(sizeof(struct node));
r->data = num;
temp->link = r;
return 0;
}
int display(struct node *q) {
struct node *temp = q;
while(temp != NULL) {
printf("data = %d\n",temp->data);
temp = temp->link;
}
return 0;
}
int sizeofl(struct node *temp) {
int count = 0;
struct node *temp1 = temp;
while(temp1 != NULL) {
count++;
temp1 = temp1->link;
}
return count;
}
int swap_list(struct node **q,int position) {
struct node *temp = *q, *ptr1 = *q,*ptr2 = *q;
int size_list,i,temp_data = 0;
size_list = sizeofl(temp);
printf("count = %d\n",size_list);
for(i=1; i<position;i++)
ptr1 = ptr1->link;
printf("ptr1 data = %d\n",ptr1->data);
for(i=0;i<size_list- position;i++)
ptr2 = ptr2->link;
printf("ptr1 data = %d\n",ptr2->data);
temp_data = ptr1->data;
ptr1->data = ptr2->data;
ptr2->data = temp_data;
return 0;
}
int main()
{
struct node *k = NULL;
append(&k,1);
append(&k,2);
append(&k,3);
append(&k,4);
append(&k,5);
append(&k,6);
append(&k,7);
append(&k,8);
append(&k,9);
display(k);
swap_list(&k,3);
display(k);
return 0;
}
@aashish yes buddy you are right.
- agmegharaj November 25, 2013I am doing this by,
1) suppose we have a source linked list, now copy the entire linked list to other linked list i,e target linked list;
2) now reverse the target linked list;
3) now check if the data in the source linked list and target linked list are equal, if they are equal they are palindrome, otherwise theya are not palindrome.
4) now free the target linked list.
I know this is not best solution, but it works. Below is the code.
#include<stdio.h>
#include<malloc.h>
struct node {
int data;
struct node *link;
};
int append_source(struct node **source,int num) {
struct node *temp,*r;
temp = *source;
if(temp == NULL) {
temp = (struct node *) malloc(sizeof(struct node));
temp->data = num;
temp->link = NULL;
*source = temp;
return 0;
}
while(temp->link != NULL)
temp = temp->link;
r = (struct node *) malloc(sizeof(struct node));
r->data = num;
temp->link = r;
r->link = NULL;
return 0;
}
int display(struct node *source) {
struct node *temp = source;
while(temp != NULL) {
printf("list data = %d\n",temp->data);
temp = temp->link;
}
return 0;
}
int copy_list(struct node **source, struct node **target) {
struct node *sou = *source,*temp = *target,*r;
while(sou != NULL) {
if(temp == NULL) {
temp = (struct node *) malloc(sizeof(struct node));
temp->data = sou->data;
temp->link = NULL;
*target = temp;
}
else {
while(temp->link != NULL)
temp = temp->link;
r = (struct node *) malloc(sizeof(struct node));
r->data = sou->data;
temp->link = r;
r->link = NULL;
}
sou = sou->link;
}
return 0;
}
int reverse_list(struct node **target) {
struct node *head = *target,*next,*cursor = NULL;
while(head != NULL) {
next = head->link;
head->link = cursor;
cursor = head;
head = next;
}
(*target) = cursor;
return 0;
}
int check_pal(struct node **source, struct node **target) {
struct node *sou = *source,*tar = *target;
while( (sou) && (tar) ) {
if(sou->data != tar->data) {
printf("given linked list not a palindrome\n");
return 0;
}
sou = sou->link;
tar = tar->link;
}
printf("given string is a palindrome\n");
return 0;
}
int remove_list(struct node *target) {
struct node *temp;
while(target != NULL) {
temp = target;
target = target->link;
free(temp);
}
return 0;
}
int main()
{
struct node *source = NULL, *target = NULL;
append_source(&source,1);
append_source(&source,2);
append_source(&source,1);
display(source);
copy_list(&source, &target);
display(target);
reverse_list(&target);
printf("list reversed\n");
display(target);
check_pal(&source,&target);
remove_list(target);
return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
char c[] = "aabbbccccddeffffgg";
char prev = c[0];int i;
for(i = 0; i< strlen(c)-1;i++) {
if((prev != c[i]) && (c[i] != c[i+1]))
printf("character %c\n",c[i]);
prev = c[i];
}
return 0;
}
#include<stdio.h>
int main()
{
char c[] = "t t h to the j enfo j o k";
char *cp = c;
int count = 0;
if(c[1] == ' ')/*checks 1st character is single*/
count++;
while((*cp) != '\0') {
if((*cp) == ' ') {
if( *(cp+= 2) == ' ') {
count++;
cp-=2;/* i used to modify the pointer, so it missed some blanks*/
}
}
cp++;
}
if(c[strlen(c) - 2] == ' ')/*checks last character is single*/
count++;
printf("count = %d\n",count);
return 0;
}
#include<stdio.h>
int main()
{
int i = 756;
char c[2] = " ";
sprintf(c,"%d",i);
printf("%s\n",c);
for(i=2;i>=0;i--)
printf("%c",c[i]);
return 0;
}
int main()
{
char string[] = "my name is megharaj I an from india";
char *stringp = string;
char sub[] = "an";
int length = strlen(sub) -1;
char *temp;
while(*stringp != '\0') {
temp = sub;
length = 2;
while((stringp != '\0') && (*stringp == *temp) && length > 0) {
stringp++;
temp++;
length--;
if(length == 0) {
printf("substring found\n");
return 0;
}
}
stringp++;
}
printf("substring not found\n");
return 0;
}
#include<stdio.h>
#include<string.h>
int countr(char *t, char *s) {
int count = 0,length,i;
length = strlen(t) - 1;
while(*s != '\0') {
for(i = 0; i<=length;i++) {
if(t[i] == *s) {
count++;
break;
}
}
s++;
}
return count;
}
int main()
{
int count;
char s[] ="hello world";
char t[] ="llld";
count = countr(t,s);
printf("%d\n", count);
return 0;
}
Here is my solution
- agmegharaj December 03, 2013