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

Write a single line c expression to delete a,b,c from aabbcc

2 Answers   Microsoft,


main() { int i; i = abc(); printf("%d",i); } abc() { _AX = 1000; }

2 Answers  


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

1 Answers   TCS,


main() { int a=2,*f1,*f2; f1=f2=&a; *f2+=*f2+=a+=2.5; printf("\n%d %d %d",a,*f1,*f2); }

6 Answers  


create a login program that ask username and password. if you input username or password 3 times wrong, the program will terminate else the program will prompt a message "congratulations"

2 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() { int i, j, *p; i = 25; j = 100; p = &i; // Address of i is assigned to pointer p printf("%f", i/(*p) ); // i is divided by pointer p } a. Runtime error. b. 1.00000 c. Compile error d. 0.00000

3 Answers   HCL,


In a gymnastic competition, scoring is based on the average of all scores given by the judges excluding the maximum and minimum scores. Let the user input the number of judges, after that, input the scores from the judges. Output the average score. Note: In case, more than two judges give the same score and it happens that score is the maximum or minimum then just eliminate two scores. For example, if the number of judges is 5 and all of them give 10 points each. Then the maximum and minimum score is 10. So the computation would be 10+10+10, this time. The output should be 10 because 30/3 is 10.

0 Answers   TCS,


main() { char *a = "Hello "; char *b = "World"; clrscr(); printf("%s", strcpy(a,b)); } a. “Hello” b. “Hello World” c. “HelloWorld” d. None of the above

4 Answers   Corporate Society, HCL,


main() { int i; clrscr(); for(i=0;i<5;i++) { printf("%d\n", 1L << i); } } a. 5, 4, 3, 2, 1 b. 0, 1, 2, 3, 4 c. 0, 1, 2, 4, 8 d. 1, 2, 4, 8, 16

4 Answers   HCL,


main() { int i=5; printf("%d",++i++); }

1 Answers  


What is the output for the program given below typedef enum errorType{warning, error, exception,}error; main() { error g1; g1=1; printf("%d",g1); }

1 Answers  


Categories