i tried but it did not work.....please help me

#include <iostream.h>
#include <conio.h>
int length_str(char word[]);
char cat_str(char word1[], char word2[]);
void main()
{
  clrscr();
  char word[50],word1[50],word2[50];
  int choice,z,y;
  do{clrscr();
  cout<<"\t\tSELECT YOUR CHOICES"
      <<"\n\t [1] - tells number of characters the string has"
      <<"\n\t [2] - Concatenation of string"
      <<"\n\t [3] - Exit"
      <<endl;
  cin>>choice;
  clrscr();
   switch(choice)
    {
      case 1: cout<<"Enter a word: ";
       cin>>word;
       z=length_str(word);
       cout<<"The no. of characters the string has is: "<<z;
       getch();
       break;
      case 2: cout<<"Enter 1st word: ";
       cin>>word1;
       cout<<"Enter 2nd word: ";
       cin>>word2;
       y=cat_str(word1,word2);
       cout<<"the concatenation word is: "<<y;
              getch
       break;
      case 3: cout<<"EXIT";
              getch
       break;
     }
   }while(choice == 1 || choice == 2);
}
int length_str(char word[])
{
  int i=0;
  for(int j=0;word[j]!='\0';j++)
  i++;
  return i;
}
char cat_str(char word1[], char word2[])
{ char word3[100];
  int i=0,hold;
  hold=for(int j=0;word1[j]!='\0';j++)
  
  for(int j=0;word2[j]!='\0';j++)
  word1[hold++]=word2[i];
  
  return y;

}
Salem commented: 15 posts, still no code tags, still using void main - obviously not paying attention -2

Recommended Answers

All 8 Replies

Member Avatar for iamthwee

>concatenate words without using STRING.H

Use #include <string> See example for brevity...

#include <iostream>
#include <string>

using namespace std;
int main()
{
  string a = "you are ";
  string b = "a retard";

  cout << a + b << endl;
  return 0;
}

If string.h isn't allowed, I'm reasonably sure std::string isn't either. This is my educated guess based on the fact that the OP is using an old compiler that probably doesn't support std::string. :icon_rolleyes:

you are required not to use the strcat and other syntax using the header file string.h


it must be a array

please help me....need it badly

Well, I guess you haven't done pointers or functions yet. Today, I also got the same question for my homework. It's actually simple, if you know/understand string Length and string Reverse (without using the library) then you'll get it quickly. I just finished with it -_-' and found myself, being silly for not thinking of it sooner (not that I didn't knew I was an idiot *sigh*) Oh! well, This program, will concate two strings (words) into a single large word.

#include<iostream>
using namespace std;
int main()
	{
	int i, j, k;
	char str1[25], str2[25], large[50];
	cout<<"Enter the first word : ";
	cin>>str1;
	fflush(stdin);
	cout<<"Enter the second word : ";
	cin>>str2;
	fflush(stdin);

	for(i=0; str1[i] != '\0'; i++)
		{
		large[i] = str1[i];
		}
	k = i;
	k--;
	for(j=0; str2[j] != '\0'; j++, k++)
		{
		large[k] = str2[j];
		}
	large[k] = '\0';

	cout<<"Concated String : ";
	cout<<large<<endl;

	cin.ignore();
	cin.get();

	return 0;
	}

> fflush(stdin);
This is wrong, very wrong.
fflush() works on
- output streams
- update streams where the last operation was a write.

Using it in any other circumstance is undefined behaviour, meaning anything can happen.

>This is wrong, very wrong.
Oh! Sorry, I shouldn't have used it here. Actually, I was trying to do this program in C, and converted it. But thanks for informing me about fflush()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.