ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip   To Refer this Site to Your Friends   Click Here
Google
 
Categories  >>  Code Snippets  >>  Programming Code  >>  C Code
 
 


 

 
 C Code interview questions  C Code Interview Questions
 C++ Code interview questions  C++ Code Interview Questions
 VC++ Code interview questions  VC++ Code Interview Questions
 Java Code interview questions  Java Code Interview Questions
 Dot Net Code interview questions  Dot Net Code Interview Questions
 Visual Basic Code interview questions  Visual Basic Code Interview Questions
 Programming Code AllOther interview questions  Programming Code AllOther Interview Questions
Question
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.
 Question Submitted By :: Coder_1
I also faced this Question!!     Rank Answer Posted By  
 
  Re: 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.
Answer
# 1
#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 ?    2 Yes 1 No
Santhoo035
[Nan]
 
  Re: 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.
Answer
# 2
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 ?    1 Yes 1 No
Ew
 
 
 
  Re: 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.
Answer
# 3
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 0 No
Rahul Shandilya
 
  Re: 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.
Answer
# 4
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 1 No
Yegullelew
 
  Re: 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.
Answer
# 5
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 ?    1 Yes 0 No
Sivan
 

 
 
 
Other C Code Interview Questions
 
  Question Asked @ Answers
 
main() { int i=-1,j=-1,k=0,l=2,m; m=i++&&j++&&k++||l++; printf("%d %d %d %d %d",i,j,k,l,m); }  1
main() { char *p = "hello world"; p[0] = 'H'; printf("%s", p); } a. Runtime error. b. “Hello world” c. Compile error d. “hello world” HCL1
1 o 1 1 0 1 0 1 0 1 1 0 1 0 1 how to design this function format in c-language ?  1
main() { int i=5; printf("%d",++i++); }  1
main() { printf("\nab"); printf("\bsi"); printf("\rha"); }  1
#include<stdio.h> main() { FILE *ptr; char i; ptr=fopen("zzz.c","r"); while((i=fgetch(ptr))!=EOF) printf("%c",i); }  1
Link list in reverse order. NetApp7
#include<stdio.h> main() { char s[]={'a','b','c','\n','c','\0'}; char *p,*str,*str1; p=&s[3]; str=p; str1=s; printf("%d",++*p + ++*str1-32); }  1
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
#ifdef something int some=0; #endif main() { int thing = 0; printf("%d %d\n", some ,thing); }  1
main() { static int var = 5; printf("%d ",var--); if(var) main(); }  1
void main() { char ch; for(ch=0;ch<=127;ch++) printf(“%c %d \n“, ch, ch); }  1
Find your day from your DOB? Microsoft12
main() { int i=_l_abc(10); printf("%d\n",--i); } int _l_abc(int i) { return(i++); }  1
What are the following notations of defining functions known as? i. int abc(int a,float b) { /* some code */ } ii. int abc(a,b) int a; float b; { /* some code*/ }  1
main() { int a=10,*j; void *k; j=k=&a; j++; k++; printf("\n %u %u ",j,k); }  1
main() { int c=- -2; printf("c=%d",c); }  1
to remove the repeated cahracter from the given caracter array. i.e.., if the input is SSAD output should of SAD Synergy6
union u { struct st { int i : 4; int j : 4; int k : 4; int l; }st; int i; }u; main() { u.i = 100; printf("%d, %d, %d",u.i, u.st.i, u.st.l); } a. 4, 4, 0 b. 0, 0, 0 c. 100, 4, 0 d. 40, 4, 0 HCL1
void main() { static int i; while(i<=10) (i>2)?i++:i--; printf(“%d”, i); }  1
 
For more C Code Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com