c++ string program

Jan 23, 2014 at 12:56pm
Heyy guyz I have made a c++ program of strings.
I want to display in my output only those words of that string which have length greater than 2.
for eg.
Is this you
OUTPUT:
this you

SUBJECT: plz correct my program logic as I am not able to get the desired output.

CODE:

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
main()
{
clrscr();
char str[100];
int count=0;
cout<<"Enter the string\n";
gets(str);
int m=strlen(str);
for(int i=-1;i<m;i++)
{
if((i==-1)||(str[i]==' '))
{
count=0;
int j=i+1;
while((str[j]!=' ')||(j!=m))
{
count++;
j++;
}

if(count>2)
{
puts(str);
break;
}
}
}
getch();
}
Last edited on Jan 23, 2014 at 1:12pm
Jan 23, 2014 at 1:25pm
Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/
Jan 23, 2014 at 1:25pm
I have made a c++ program of strings.

Can't really see much C++ in there to be honest.


The structure of your program should be more like this:

1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
#include<string>

int main()
{

        .....
        .....
	
	return 0;
}


iostream.h is very old. Here's some info: http://members.gamedev.net/sicrane/articles/iostream.html

Do you have to use char arrays? If you could use std::string that would make your life easier.

and can you use code tags when you post your code as well, cheers.
Last edited on Jan 23, 2014 at 1:26pm
Jan 23, 2014 at 2:02pm
I agree with the above. You could do this quite nicely with C++ strings and vectors.
Jan 23, 2014 at 3:36pm
try to use...

1
2
3
4
5
6
7
8
 
#include<cstring.h>

string mystring;

getline(cin,mystring); // To insert string



we used #include<stdio.h> in C
Jan 23, 2014 at 3:42pm
Um... no! If you're using the STL string class, you want:

#include<string>

NOT:

#include<cstring.h>
Jan 23, 2014 at 3:45pm
MikeyBoy

BUT in my complier it doesn't work.. #include<cstring.h> works in stead
Jan 23, 2014 at 3:48pm
If you're using STL it will be in:

#include <string>

Which compiler are you using?
Jan 23, 2014 at 3:52pm
i am using Turbo C++ v4.5
Jan 23, 2014 at 3:57pm
Well, there you go a 20 year old compiler!!!!
Jan 23, 2014 at 4:00pm
ohhhhhhhhhhhh...Ouch .>We are taught with Turbo C 3.0 C programming :D The very basic.
Jan 24, 2014 at 9:57am
Kindly ignore the indentation please..
And I would prefer the basic solution for it rather than looking forward to advanced string solution of it.
Jan 24, 2014 at 11:23am
Ok, without using std::string:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
using namespace std;

#define MAX_STRING 100
#define MAX_WORDS 50

int get_words(char* str, char** words)
{
   int word_count(0);
   char* p = strtok(str, " ");
   while (p)
   {
      words[word_count] = new char[strlen(p)+1];
      strcpy(words[word_count++], p);
      p = strtok(NULL, " ");
   }

   return word_count;
}

void output_words(char** words, const int word_count, const int greater_than)
{
   cout << endl << endl;

   for (int idx = 0; idx < word_count; ++idx)
   {
      if (strlen(words[idx]) > greater_than)
         cout << words[idx] << endl;
   }
}

int main()
{
   char str[MAX_STRING] = {0};
   cout << "Enter string" << endl;
   cin.getline(str, MAX_STRING, '\n');
   char* words[MAX_WORDS] = {NULL};
   const int word_count = get_words(str, &words[0]);
   output_words(words, word_count, 2);
	
   for (int idx = 0; idx < word_count; ++idx)
      delete[] words[idx];

   return 0;
}
Last edited on Jan 24, 2014 at 11:23am
Jan 24, 2014 at 1:55pm
Thank you so much ajh32. :)
Topic archived. No new replies allowed.