Jan 23, 2014 at 12:56pm Jan 23, 2014 at 12:56pm UTC
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:12pm UTC
Jan 23, 2014 at 1:25pm Jan 23, 2014 at 1:25pm UTC
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 1:26pm UTC
Jan 23, 2014 at 2:02pm Jan 23, 2014 at 2:02pm UTC
I agree with the above. You could do this quite nicely with C++ strings and vectors.
Jan 23, 2014 at 3:42pm Jan 23, 2014 at 3:42pm UTC
Um... no! If you're using the STL string class, you want:
#include<string>
NOT:
#include<cstring.h>
Jan 23, 2014 at 3:45pm Jan 23, 2014 at 3:45pm UTC
MikeyBoy
BUT in my complier it doesn't work.. #include<cstring.h>
works in stead
Jan 23, 2014 at 3:48pm Jan 23, 2014 at 3:48pm UTC
If you're using STL it will be in:
#include <string>
Which compiler are you using?
Jan 23, 2014 at 3:52pm Jan 23, 2014 at 3:52pm UTC
i am using Turbo C++ v4.5
Jan 23, 2014 at 3:57pm Jan 23, 2014 at 3:57pm UTC
Well, there you go a 20 year old compiler!!!!
Jan 23, 2014 at 4:00pm Jan 23, 2014 at 4:00pm UTC
ohhhhhhhhhhhh...Ouch .>We are taught with Turbo C 3.0 C programming :D The very basic.
Jan 24, 2014 at 9:57am Jan 24, 2014 at 9:57am UTC
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 Jan 24, 2014 at 11:23am UTC
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 11:23am UTC
Jan 24, 2014 at 1:55pm Jan 24, 2014 at 1:55pm UTC
Thank you so much ajh32. :)