Write a prog to accept a given string in any order and flash
error if any of the character is different.

For example : If abc is the input then abc, bca, cba, cab
bac are acceptable, but aac or bcd are unacceptable.

Answers were Sorted based on User's Feedback



Write a prog to accept a given string in any order and flash error if any of the character is diffe..

Answer / santhoo035

#include<iostream.h>
#include<string.h>
int main()
{
int flag=0,i=0,j=0;
char ar[4]="abc" ;
char temp[4];
cout<<"Plz enter a string with size #3";
cin>>temp;
for(j=0;ar[j];j++)
{
flag=0;
for(i=0;temp[i];i++)
{
if(ar[j]==temp[i])
flag+=1;
}
if(flag==0||flag>1)
break;
}
if(flag==0||flag>1)
cout<<"Not accepted";
else
cout<<"Accepted";
}
Note:PLz convert "iostream.h" into "stdio.h" and "cout<<"
to "printf"

Is This Answer Correct ?    6 Yes 4 No

Write a prog to accept a given string in any order and flash error if any of the character is diffe..

Answer / ew

The above is broken. If the ar[4] is "aac" rather
than "abc" it fails.

#include "stdio.h"

#define CountOf(a) (sizeof(a)/sizeof(*a))

int main(int argc, _TCHAR* argv[])
{


char data[] = "abc";
char test[CountOf(data)];

printf("Enter %d characters: ", CountOf(data)-1);
scanf("%s", test);

for (int i=0;i < CountOf(data);i++)
{
for (int j=0;j < CountOf(data);j++)
{
if (data[i] == test[j])
{
test[j] = 0;
break;
}
}
}

for (int i=0;i < CountOf(data);i++)
{

if (test[i]) {
printf("\nNot ");
break;
}
}
printf("\nAccepted");

return 1;
}

Is This Answer Correct ?    2 Yes 2 No

Write a prog to accept a given string in any order and flash error if any of the character is diffe..

Answer / rahul shandilya

both of the solutions above given have complexity O(N^2),
i have tried to solve it in O(N).let me inform if there is
any bug or better solution .

#include<iostream>
using namespace std;
int main()
{
int a[130],a1[130];
memset(a,0,sizeof(a));
memset(a1,0,sizeof(a));
char ans[1000];
char str[]="putanystringhere";
for(int i=0;i<sizeof(str)/sizeof(char)-1;i++)
{
a[(int)str[i]]+=1;
}
cout<<"Enter any string of length
"<<sizeof(str)/sizeof(char)-1<<" :";
cin>> ans;
for(int i=0;i<sizeof(str)/sizeof(char)-1;i++)
{
a1[(int)ans[i]]+=1;
}
bool flag=true;
for(int i=0;i<sizeof(str)/sizeof(char)-1;i++)
{
if(a[(int)str[i]]!=a1[(int)str[i]])
{
flag=false;
break;
}
}
if(flag==true)
cout<<"No error";
else
cout<<"Error";
system("pause");
return 0;
}

Is This Answer Correct ?    1 Yes 3 No

Write a prog to accept a given string in any order and flash error if any of the character is diffe..

Answer / sivan

if we ask the user to enter the second string having the
same characters as in the first string, the program
essentailly reduces to finding whether there is a
repetition in the second string. A program for this is
given below.

#include<iostream>
using namespace std;

int main()
{
char string1[10];
char string2[10];
int flag = 0;

cout<<"\n Enter the first string";
cin>>string1;
cout<<"\n Enter the second string with the same
characters as entered in string1";
cin>>string2;
for(int i = 0; i < strlen(string2); i++)
{
for(int j = i+1; j < strlen(string2) ; j++)
{
if(string2[i] == string2[j])
{
flag = 1;
break;
}
}
}

if(flag ==1)
cout<<"\n wrong";
else
cout<<"\n correct";

return 0;
}

Is This Answer Correct ?    2 Yes 4 No

Write a prog to accept a given string in any order and flash error if any of the character is diffe..

Answer / yegullelew

My answer is O(n) - but it is done in C# sorry for C guys
public bool isAccepted(string original, string
toCompare)
{
if (original.Length != toCompare.Length)
return false;
bool[] flags = new bool[128];
for (int i = 0; i < toCompare.Length; i++)
flags[toCompare[i]] = true;
for (int i = 0; i < original.Length; i++)
if (!flags[original[i]])
return false;
return true;
}

Is This Answer Correct ?    1 Yes 4 No

Post New Answer

More C Code Interview Questions

main() { int a[10]; printf("%d",*a+1-*a+3); }

1 Answers  


Write a routine that prints out a 2-D array in spiral order

3 Answers   Microsoft,


main() { extern int i; i=20; printf("%d",i); }

1 Answers   Value Labs,


main() { static int a[3][3]={1,2,3,4,5,6,7,8,9}; int i,j; static *p[]={a,a+1,a+2}; for(i=0;i<3;i++) { for(j=0;j<3;j++) printf("%d\t%d\t%d\t%d\n",*(*(p+i)+j), *(*(j+p)+i),*(*(i+p)+j),*(*(p+j)+i)); } }

1 Answers  


#include<stdio.h> main() { struct xx { int x=3; char name[]="hello"; }; struct xx *s; printf("%d",s->x); printf("%s",s->name); }

3 Answers   Hexaware,






main() { float f=5,g=10; enum{i=10,j=20,k=50}; printf("%d\n",++k); printf("%f\n",f<<2); printf("%lf\n",f%g); printf("%lf\n",fmod(f,g)); }

1 Answers  


posted by surbhi just now main() { float a = 5.375; char *p; int i; p=(char*)&a; for(i=0;i<=3;i++) printf("%02x",(unsigned char) p[i]); } how is the output of this program is :: 0000ac40 please let me know y this output has come

2 Answers   GATE,


main() { int i=5; printf(“%d”,i=++i ==6); }

1 Answers  


program to Reverse a linked list

12 Answers   Aricent, Microsoft, Ness Technologies,


main() { char name[10],s[12]; scanf(" \"%[^\"]\"",s); } How scanf will execute?

2 Answers  


Is there any difference between the two declarations, 1. int foo(int *arr[]) and 2. int foo(int *arr[2])

1 Answers  


main() { { unsigned int bit=256; printf("%d", bit); } { unsigned int bit=512; printf("%d", bit); } } a. 256, 256 b. 512, 512 c. 256, 512 d. Compile error

1 Answers   HCL,


Categories