?
Solved

String Segmentation (This project is urgent!! It is to be dued next day! Please reply soon.)

Posted on 2003-03-26
17
Medium Priority
?
329 Views
Last Modified: 2008-03-17
short transmit(char *pMss, int leng)
{

  char *pSS={"\x0b"};
  char *pLL;


  strncpy(pLL,pMss,leng);
  printf("%c",pLL);
  getch();
  *pLL=*pSS+*pLL
  strcpy ((char *)pTxBuffer->BitT.comm_buffer, pLL);



  // Initialize the transmit buffer comm_block

  pTxBuffer->BitT.buffer_length = strlen((char*)pTxBuffer->BitT.comm_buffer);
  pTxBuffer->BitT.buffer_status = 0;
  pTxBuffer->BitT.idle_flag =  0;

#ifdef __OS2__
  rc = sync_transmit (pMPA_cfg, pTxBuffer, 10000);
#elif defined _WINDOWS
  rc = sync_transmit (pMPA_cfg, pTxBuffer, 0);
#else  // DOS
  rc = sync_transmit (pMPA_cfg, pTxBuffer);
#endif

  if (rc)
     {
     printf("Error starting frame transmission %04x", rc);
     return(-1);
     }

  while (!(pTxBuffer->BitT.buffer_status & (TxDone | TxAbort | TxError)));

  if (pTxBuffer->BitT.buffer_status & TxError)
     printf("Transmitter Error\n");
  if (pTxBuffer->BitT.buffer_status & TxAbort)
     printf("Transmitter underrun\n");

  return(pTxBuffer->BitT.buffer_status);

}
#define MAXLEN 50
void split(char *pMessage)
{
 int slen, leng;

 char *pE, *pF;

 slen = strlen(pMessage) ;
 pE = pMessage;
 do
 {
           pF = pE;
       leng = slen > MAXLEN ? MAXLEN : slen ;

       pE = pF + leng;

       while (*pE != ' ')
       {
              if (*pE == 0)
        break ;
            pE--;
        leng-- ;
       }

         leng=pE-pF ;

     printf("%*.*s\n", leng, leng, pF) ;

     getch() ;

     transmit(pF, leng);

     slen -= leng;

 } while(slen>0);

}


This is a function to segment the string into 50 by 50 characters. By starting from the second sentence there is a spacing in front of the segmented string. How can i remove the space?
0
Comment
Question by:bblovedai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 4
  • +1
17 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 8210223
Talk about strange co-incidences. Its the second time today that I've seen two questions with over 100 lines of code and exactly the same to every semi-colon as that posted by somebody else, asking almost the same question! A question by some bbsts (if that's the correct name) had exactly the same code.

Mayank.
0
 

Author Comment

by:bblovedai
ID: 8210301
bbtss is my project mate. we are working on this project.
i hope u won't mind helping us as soon as poss. thanks a lot!
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8210336
That's a very long code.... I already posted him some answers. What do you exactly want to know about? I don't understand your 'spacing in front of the segmented string'.

Mayank.
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:bblovedai
ID: 8210503
For example, the message is:
bbtss is my project mate. we are working on this project. i hope u won't mind helping us as soon as poss. thanks a lot!

The function will then splits this message into 50 by 50 characters:

bbtss is my project mate. we are working
 on this project. i hope u won't mind helping us
 as soon as poss. thanks a lot!
 

The last 2 lines of data are not aligned with the 1st line due to spacing between words. By starting from the second line, how can i remove the space?
0
 

Author Comment

by:bblovedai
ID: 8210522
For example, the message is:
bbtss is my project mate. we are working on this project. i hope u won't mind helping us as soon as poss. thanks a lot!

The function will then splits this message into 50 by 50 characters:

bbtss is my project mate. we are working
  on this project. i hope u won't mind helping us
  as soon as poss. thanks a lot!
 

The last 2 lines of data are not aligned with the 1st line due to spacing between words. By starting from the second line, how can i remove the space?
0
 

Author Comment

by:bblovedai
ID: 8210533
For example, the message is:
bbtss is my project mate. we are working on this project. i hope u won't mind helping us as soon as poss. thanks a lot!

The function will then splits this message into 50 by 50 characters:

bbtss is my project mate. we are working
   on this project. i hope u won't mind helping us
   as soon as poss. thanks a lot!
 

The last 2 lines of data are not aligned with the 1st line due to spacing between words. By starting from the second line, how can i remove the space?
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 160 total points
ID: 8210593
Remove it by shifting all the characters one position back. Let's say that this is stored in an allocated string (character array) 'str'.

if ( str[0] == ' ')
{
  int len = strlen ( str ) ;
  char * temp = new char[len+1] ;
  stcpy ( temp, str ) ;
  str = realloc ( len ) ;
 
  for ( int i = 0 ; i < len - 1 ; i ++ )
    str[i] = temp[i+1] ; // end for

  str[i] = '\0' ;

} // end if

Mayank.
 
0
 
LVL 6

Accepted Solution

by:
gj62 earned 220 total points
ID: 8210783
try this:

#include <string.h>
if ( str[0] == ' ')
{
 memmove(str, str+1, strlen(str));
}
0
 
LVL 6

Expert Comment

by:gj62
ID: 8210796
Oh, the above code just shifts the string 1 byte to the left...  if you have more than one space, just put it in a while loop...

while (str[0] == ' ')
{
  memmove(str, str+1, strlen(str));
}

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8210844
gj62,

Does memmove () deallocate the memory for the extra-character?? I used the realloc () so that the memory for the extra character is removed..

Mayank.
0
 
LVL 6

Expert Comment

by:gj62
ID: 8210968
No, but do you need to?

Actually, now that I took time to read from the beginning, neither solutions will work well...

They aren't really manipulating the string, they are just printing it out section by section...

Here's a modification to their original function:

#define MAXLEN 50
void split(char *pMessage)
{
int slen, leng;

char *pE, *pF;

slen = strlen(pMessage) ;
pE = pMessage;
do
{
          pF = pE;
      leng = slen > MAXLEN ? MAXLEN : slen ;

      pE = pF + leng;

      while (*pE != ' ')
      {
             if (*pE == 0)
       break ;
           pE--;
       leng-- ;
      }

    /* REMOVE leading spaces */
    while (pF[0] == ' ')
    {
      pF++;
    }

    leng=pE-pF ;

    printf("%*.*s\n", leng, leng, pF) ;

    getch() ;

    transmit(pF, leng);

    slen -= leng;

} while(slen>0);
0
 
LVL 6

Expert Comment

by:gj62
ID: 8210979
Sorry, I just added:

   /* REMOVE leading spaces */
   while (pF[0] == ' ')
   {
     pF++;
   }

to advance the pointer if there were space(s) at the beginning of *pF...

It really doesn't remove them, so a better comment would be /*SKIP leading spaces*/ -

Since all that is being done is printf this should work just dandy...
0
 

Author Comment

by:bblovedai
ID: 8212655
dear mayankeagle

can u kindly take a look at this question again that bbtss asked,
Title: Low points tough question please help!!! SYNCDRIVE.
my team mate has added a new comment to ask u again.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8215432
I have seen it....

Mayank.
0
 

Expert Comment

by:CleanupPing
ID: 9446765
bblovedai:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9451880
Splt points between mayankeagle and gj62.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 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