9 Replies - 3676 Views - Last Post: 19 September 2012 - 01:49 PM Rate Topic: -----

#1 madhu9124  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-September 12

error while finding common alphabets in string

Posted 19 September 2012 - 12:29 PM

Hi,
Question is:to find common alphabets in AMITABH BACHCHAN AND RAJNIKANTH using c++

code is:
#include<iostream.h>
#include<conio.h>
#include<string.h>
int main()
{
  clrscr();
  char string1[20],string2[15];
  int i,j;
    string1="AMITABH BACHCHAN";
    string2="RAJNIKANTH";
  int  n1=strlen(string1);
  int n2=strlen(string2);
  cout<<"common alphabets in AMITABH BACHCHAN AND RAJNIKANTH are-";
    for(i=0;i<n1;i++)
    {
      for(j=0;j<n2;j++)
      {
        if(string1[i]==string2[j])
          cout<<string[i];
      }
    }
}



but i get error while compiling

Thank You

Madhu

Is This A Good Question/Topic? 0
  • +

Replies To: error while finding common alphabets in string

#2 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • Posts: 4,163
  • Joined: 08-January 12

Re: error while finding common alphabets in string

Posted 19 September 2012 - 12:38 PM

if(string1[i]==string2[j])
          cout<<string[i];

Where have you defined string? I think it should be string1.
Was This Post Helpful? 0
  • +
  • -

#3 madhu9124  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-September 12

Re: error while finding common alphabets in string

Posted 19 September 2012 - 12:49 PM

View Postaresh, on 19 September 2012 - 12:38 PM, said:

if(string1[i]==string2[j])
          cout<<string[i];

Where have you defined string? I think it should be string1.

I need to print the common alphabets after comparing,so i think string1 might not be the correct way to use in cout statement right?

View Postmadhu9124, on 19 September 2012 - 12:45 PM, said:

View Postaresh, on 19 September 2012 - 12:38 PM, said:

if(string1[i]==string2[j])
          cout<<string[i];

Where have you defined string? I think it should be string1.

I need to print the common alphabets after comparing,so i think string1 might not be the correct way to use in cout statement right?

i changed string to string1 as you said,but i get undefined symbol string1,string2.How do i solve this error?
Was This Post Helpful? 0
  • +
  • -

#4 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • Posts: 4,163
  • Joined: 08-January 12

Re: error while finding common alphabets in string

Posted 19 September 2012 - 12:56 PM

Well, if you just changed string to string1 on line 20 and you got an error, your compiler is nuts.

However, on line 10 and 11, you must use strcpy while trying to copy the names to string1 and string2.

This post has been edited by aresh: 19 September 2012 - 12:59 PM

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3483
  • View blog
  • Posts: 10,734
  • Joined: 05-May 12

Re: error while finding common alphabets in string

Posted 19 September 2012 - 01:12 PM

View Postaresh, on 19 September 2012 - 12:56 PM, said:

Well, if you just changed string to string1 on line 20 and you got an error, your compiler is nuts.

Were you expecting better from Turbo C? :)
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3483
  • View blog
  • Posts: 10,734
  • Joined: 05-May 12

Re: error while finding common alphabets in string

Posted 19 September 2012 - 01:23 PM

Assuming you get your compilation issues figured out, doesn't your current code print out duplicates of each of the letters found in common? For example, I think that it will print out 'A' about 8 times.
Was This Post Helpful? 0
  • +
  • -

#7 madhu9124  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-September 12

Re: error while finding common alphabets in string

Posted 19 September 2012 - 01:40 PM

#include<iostream.h>
#include<conio.h>
#include<string.h>
int main()
{
  
 
  int i,j;
    char a[30]="AMITABH BACHCHAN";
    char b[40]="RAJNIKANTH";
  int  n1=strlen(a);
  int n2=strlen(B)/>;
  cout<<"common alphabets in AMITABH BACHCHAN AND RAJNIKANTH are-";
    for(i=0;i<n1;i++)
    {
      for(j=0;j<n2;j++)
      {
        if(a[i]==b[j])
          cout<<a[i];
      }
    }

Changed the program,but compilation is successfull,but no output


Was This Post Helpful? 0
  • +
  • -

#8 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • Posts: 4,163
  • Joined: 08-January 12

Re: error while finding common alphabets in string

Posted 19 September 2012 - 01:46 PM

Are you sure there's no output? It should give the correct output, because the code is correct.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3483
  • View blog
  • Posts: 10,734
  • Joined: 05-May 12

Re: error while finding common alphabets in string

Posted 19 September 2012 - 01:47 PM

He just probably needs a cout << endl; at the end of his program to flush the output buffer.
Was This Post Helpful? 0
  • +
  • -

#10 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • Posts: 4,163
  • Joined: 08-January 12

Re: error while finding common alphabets in string

Posted 19 September 2012 - 01:49 PM

Actually, the name suggests a "she" :P
And maybe s/he does not have getch()* at end to hold the output screen open...

*getch() because s/he is already using conio.h and clrscr()
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1