Link to home
Start Free TrialLog in
Avatar of redgreenred
redgreenred

asked on

string comparison

//trying to compare a word from a string at specific position.
//steps involve:
//(Step 1) storing starting and ending address of words in arrays start_address[] and end_address[]
//(Step 2) retrieving substring from ch2 from the perticular position
//(Step 3) compairing substring ch3 with ch1.
//(Step 4) if equal strings then retun true else return false
//Now arrays contain starting and ending addresses.
//retrieve substring from the string ch2 is not working
//getting this error message
//error C2224: left of '.substr' must have struct/union type

//my problem can be solved by getting substring

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

int words ( char sentence[]);
int match ( char str[], char sentence[], int word_no);
int count_startaddress(int k, char sentence[]);
int find_startaddress( char str[], char sentence[], int word_no);
getsubstring(int);

int start_address[15];
int end_address[15];
char ch1[]="to";
char ch2[]="welcome to the robotics department of \n you";


char *ch3;//sub string
int indexe=0, indexs=0;


void main()
{
int tot, a;
int totalwords ;


int w_no = 1;
totalwords= words(ch2);

tot = match(ch1, ch2, w_no);
find_startaddress( ch1, ch2, w_no);





printf(" \n = %d" , tot);
if (tot == 0)
      printf("\n match is found");
else
      printf("\n match is not found");

for (a= 0 ; a<=totalwords-1 ; a++)
{
      printf("\n starting word %d = %d", a+1, start_address[a]);
      printf("\n ending word %d = %d", a+1, end_address[a]);

}



getsubstring(w_no);

getch();
}

getsubstring(int w_no)
{
//this part is not working

//*ch3.substr(start_address[3], end_address[3]-start_address[3]);
      

}

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' )
                  if( sentence[i-1]!=' ' && sentence[i-1]!='\t' && sentence[i-1]!='\n')
                             wordcount++;


      return wordcount ;
}



int match ( char str[], char sentence[], int word_no)
{
      int i , wordcount=0 , length =  strlen(sentence);

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

               if( sentence[i]==' '&& i!=0 || sentence[i]=='\0' )
                  if( sentence[i-1]!=' ' && sentence[i-1]!='\t' && sentence[i-1]!='\n')
                       {
                        wordcount++;
                        end_address[indexe++] = i;

                  }
            

      return wordcount ;
}

int count_startaddress(int k, char sentence[])
{

      int value  = 0;
      int l;


      for (l=k; l>=0; )
      {

            //printf("\n value of l = %d and character = %c", k, sentence[l]);      
            if(sentence[l] == ' ' && sentence[l+1] != ' ')
            {      
                  value = l+2;
                  printf("\n value of l = %d and character = %c", l, sentence[l]);
                  break;
            }
            --l;
      }

      return value;
}


int find_startaddress( char str[], char sentence[], int word_no)
{
      int i , wordcount=0 , length =  strlen(sentence);

      for ( i=0 ; i<=length ; i++)
      {
            if (i==0 && sentence[i]!=' ')
            {
                  start_address[indexs++] = 1;
            }

               if( sentence[i]==' '&& i!=0)
                  if( sentence[i+1]==' ' || sentence[i+1]=='\t' || sentence[i+1]!='\n' )
                     {
                  
                        start_address[indexs++] = i+2;

                  }
            
      }
      return wordcount ;
}
Avatar of graham_k
graham_k

well, chr3 is a char *, but by using a dot operator, you are treatinmg it as a structure.

If
ch3.substr(h3.substr(start_address[3], end_address[3]-start_address[3]); , end_address[3]-start_address[3]);

means take a substring of ch3 starting at start_address[3] which is end_address[3]-start_address[3] bytes long, use


char *ch4;

strncpy(ch3, &ch3[start_address[3] ],  end_address[3]-start_address[3]);


Take a look at strings.h, you'll find a bunch of useful string manipulation routines.

strncpy(dest, src, len) is used above; you might also want to look at strstr(string_to_search, look_for_this) to help you locate your substring, rather than for looping your way through.

In fact, I think that what you have written above could probably all be condensed into a few lines of code.
Avatar of redgreenred

ASKER

char *ch4;

strncpy(ch3, &ch3[start_address[3] ],  end_address[3]-start_address[3]);

I think you are trying to suggest this instead of above

char *ch4;

strncpy(ch4, &ch3[start_address[3] ],  end_address[3]-start_address[3]);

yes, sorry, the destination of  strncpy() should have been the new varianble ch4.As I said, you can probably make the whole thing much simpler. But firstly, look into the string functions.

Here's a good URL for man pages http://www.linuxcentral.com/linux/man-pages/index.html

and here's one for strncpy() itself
http://www.opengroup.org/public/pubs/online/7908799/xsh/strncpy.html

go bak to the index & search for anything you want, or click on the link near the top of the page to learn all about string.h
ASKER CERTIFIED SOLUTION
Avatar of nandi
nandi

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