Link to home
Start Free TrialLog in
Avatar of tparvaiz

asked on

c to C++, simple application

I want this code to be converted from C to C++, what I am lokking is to convert array of character (since we do not have strings in c) to be converted to string. e.g. "char searchstr[30]" to be replaced with string searchstr.

please edit this code and briefly expalin any one line as how you did it.


#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include <conio.h>
int words ( char sentence[]);
int match(char str[] , char sentence[] , int word_no);

void main(void)
int ans ;
char searchstr[30]="left", sent[81]="Proceed three units forward and turn left" ;
//char searchstr[30]="robot", sent[81]="Welcome  to   the  \n\t\t\t robotics department" ;
//char searchstr[30]="*r2-d2*", sent[81]="Good    morning   \n\t  *R2-D2* & C3-p0  " ;
//char searchstr[30]="*r2-d2* & C3-p0", sent[81]="Good    morning   \n\t  *R2-D2* & C3-p0  " ;
//char searchstr[30]="H.A.L. 9000", sent[81]="Hello H.A.L.      9000" ;

ans= match( searchstr , sent ,7);
if( ans ==0 )
      printf("\n\t False");
else if ( ans == 1)
      printf("\n\t True");



int match(char str[] , char sentence[] , int word_no)
char *ptr , *ptr1;
int words_in_the_sentence = 0 , wordcount=0;
int i=0 ,j=0 , point=0 , k=0 , length = strlen(sentence);

   words_in_the_sentence= words(sentence); // Call to the other function that we have made

      ptr = strstr(strupr(sentence), strupr(str));
   printf("The value of the pointer = %x" , ptr);
      if (ptr == NULL || str[0] == ' ' || words_in_the_sentence < word_no || word_no==0)
   printf(" I am returning without going through the entire code\n") ;
      return 0 ;

   ptr1 = ptr ;
      ptr1=ptr1 + strlen(str) ;

   printf("\n The last char pointed to by the new pointer %c" ,*ptr1 );
   if(*ptr1!=' ' && *ptr1!='\t' && *ptr1!='\n' && *ptr1!='\0'  )
         printf("\n Exiting because the argument is incomplete\n");
         return 0 ;

   for ( i=0 ; i<=length ; i++)

         if( sentence[i]==' '&& i!=0 || sentence[i]=='\0' ||  sentence[i]=='\t' || sentence[i]=='\n' )
            if( sentence[i-1]!=' ' && sentence[i-1]!='\t' && sentence[i-1]!='\n')
                        printf("\n Wordcount = %d" , wordcount);
               if ( wordcount == word_no )
               printf("\n The last character in the search string is = %c" , sentence[k-1]);
               i=length ;


           for( point=k-1 ; sentence[point]!=' '; point--);  // Moving back the required number of characters
      printf("\n\t The next character is %c" , sentence[point+1]);

   printf("The value of address given by manual calculation = %x" , &sentence[point+1]);
 if(ptr == &sentence[point+1])
 return 1;
 return 0;



int words ( char sentence[])
int i , wordcount=0 , length =  strlen(sentence);

for ( i=0 ; i<=length ; i++)

         if( sentence[i]==' '&& i!=0 || sentence[i]=='\0'|| sentence[i]=='\t' || sentence[i]=='\n')
            if( sentence[i-1]!=' ' && sentence[i-1]!='\t' && sentence[i-1]!='\n')

return wordcount ;
Avatar of ddelvecc

Strings are not too hard to use, so it would probably be easier if I explained some of the syntax and then you could apply it to your program.  You don't have to give strings a length, they can dynamically change size over the course of a program.  So the second line in main could look something like this:

string searchstr="left", sent="Proceed three units forward and turn left";

which would initialize 2 strings.  Your functions would take string arguments (similar to if string were a built-in type):

int words (string sentence);

To access the length of the string you do stringName.length(), for instance, for the string searchstr, the line:
will return 4.  You can still use brackets to access an individual character of a string (as in searchstr[0] = 'd';).  The function find can be used to find a substring or individual character.  Find returns the index of the first letter of the first occurance in the substring.  For instance:
will return 2.  If the character or substring is not found, a value larger than the length of the string is return (so the value returned is not a legal index).  I'm not sure what the strupr(char *) function you use does (I think it converts a string to upper case).  You can convert an individual character to upper case with the toupper function defined in ctype.h.  Strings can also be added together with the + operator, for instance:
string temp = searchstr + " hand";
will store "left hand" in the string temp.  Strings cannot be printed with printf, cout must be used:
cout<<temp;  //prints "left hand"
Finally, for any of this to work you have to #include <string> (without the .h).
ddelvecc wrote:
> Finally, for any of this to work you have to #include <string> (without the .h).
And, of course, to use strings you have to do one of the following:

1. Prefix each instance of "string" with "std::" (that is, the name is really std::string), or

2. After the #include <string>, bring std::string into your own (the global, in this case) namespace by writing
        using std::string;
then you can simply write string instead of std::string.

(There's a 3rd option but we'll skip that)

And of course, the same goes for cout, you have to #include <iostream> (instead of stdio.h) and you have to write either std::cout, or bring cout into your namespace with a using std::cout;

You should also pass your string by const & to the words function, since the sentence is not modified there, that is, define words as
      int words(std::string const &sentence) { /*...*/ }
That avoids copying the entire string, which might be expensive.

And you really dont _have_ to modify the strings in match either (afaics), so I suggest you don't and pass by reference there as well.

Why do you have two getch() at the end of match? They'll never get called.
Avatar of Pritesh

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial