Solved

string comparison

Posted on 2000-05-14
4
393 Views
Last Modified: 2008-02-20
//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 ;
}
0
Comment
Question by:redgreenred
  • 2
4 Comments
 
LVL 6

Expert Comment

by:graham_k
ID: 2809238
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.
0
 

Author Comment

by:redgreenred
ID: 2811113
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]);

0
 
LVL 6

Expert Comment

by:graham_k
ID: 2812705
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
0
 
LVL 2

Accepted Solution

by:
nandi earned 50 total points
ID: 2890485
Hi

I have got a snippet of similar thing may be of help.

Thx
Ranjan
/* +++Date last modified: 05-Jul-1997 */

/*
**  stptok() -- public domain by Ray Gardner, modified by Bob Stout
**
**   You pass this function a string to parse, a buffer to receive the
**   "token" that gets scanned, the length of the buffer, and a string of
**   "break" characters that stop the scan.  It will copy the string into
**   the buffer up to any of the break characters, or until the buffer is
**   full, and will always leave the buffer null-terminated.  It will
**   return a pointer to the first non-breaking character after the one
**   that stopped the scan.
*/

#include <string.h>
#include <stdlib.h>
#include "snip_str.h"

#if defined(__cplusplus) && __cplusplus
 extern "C" {
#endif

char *stptok(const char *s, char *tok, size_t toklen, char *brk)
{
      char *lim, *b;

      if (!*s)
            return NULL;

      lim = tok + toklen - 1;
      while ( *s && tok < lim )
      {
            for ( b = brk; *b; b++ )
            {
                  if ( *s == *b )
                  {
                        *tok = 0;
                        for (++s, b = brk; *s && *b; ++b)
                        {
                              if (*s == *b)
                              {
                                    ++s;
                                    b = brk;
                              }
                        }
                        return (char *)s;
                  }
            }
            *tok++ = *s++;
      }
      *tok = 0;
      return (char *)s;
}

#if defined(__cplusplus) && __cplusplus
 }
#endif

#ifdef TEST

#include <stdio.h>

main(int argc, char *argv[])
{
      char *ptr, buf[256];

      if (3 > argc)
      {
            puts("Usage: STPTOK \"string\" \"token_string\"");
            return EXIT_FAILURE;
      }
      else  ptr = argv[1];
      do
      {
            ptr = stptok(ptr, buf, sizeof(buf), argv[2]);
            printf("stptok(\"%s\", \"%s\")\n  buf: \"%s\"\n  "
                  "returned: \"%s\"\n", argv[1], argv[2], buf, ptr);
      } while (ptr && *ptr);
      return EXIT_SUCCESS;
}

#endif /* TEST */
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
cURL: stopping a http transaction before it's finished 3 116
Passing command line arguments in C 10 85
Detect CR LF to each line 12 153
IIS Log files on Exchange 2013 server 6 84
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now