Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 715
  • Last Modified:

printf

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();
      slen -= leng;
  } while(slen>0);

}
In the above function,
regarding this command: printf("%*.*s\n", leng, leng, pF) ;
Ques: What this command do in this function? What *.* means?
0
bbtss
Asked:
bbtss
  • 16
  • 3
  • 2
  • +1
1 Solution
 
Mayank SAssociate Director - Product EngineeringCommented:
>>  leng==pE-pF

I guess you intend to do:

leng = pE - pF ;

Now, %*.*s will print out 'leng' number of characters from the starting of the pF.

e.g., if pF points to: "YourName" and leng = 5, then:

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

will print:

YourN

Sameway, writing:

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

will print leng characters in total, leng - 1 characters from the string, and the remaining are filled with blanks. In fact, first the blanks are printed, and then the characters, like:

 Your

So, in:

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

The first leng refers to the total number of characters to be printed, the second leng refers to the number of characters to be printed from the string pointed to by pF (and in case this number is smaller, then first, blanks will be printed).

I hope you got it.

Mayank.
0
 
bbtssAuthor Commented:
thanks for ur reply.
but is there any way that i can assign a pointer to this segmented string other than directly printf it out?
like the message is Yourname
this function will printf Yourn if leng is 5.
how can i assign a pointer to this segmented message?
0
 
lowmakaCommented:
It looks like you are trying to "extract" from a space-delimited string and proccess(transmit) them one "element" at a time. The element string should be from pC+1 to pE in the following code:

==========================================
 char *pE, *pF, *pC;

 slen = strlen(pMessage) ;
 slen = slen > MAXLEN ? MAXLEN : slen ;

 pF = pMessage;
 pE = pF + slen - 1;
 pC = pE;

 do
 {
       while (*pC != ' ')
       {
        if (*pC == 0) break ;
        pC--;
       }

       leng = pE - pC;

     printf("%*.*s\n", leng, leng, pC+1);

     getch() ;

     transmit();

     --pC;
     pE = pC;
     slen -= (leng+1);

 } while(slen>0);
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
bbtssAuthor Commented:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data .................................."};
void display(char *pMss)
{
 printf("%s",pMss);
 getch();
}
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();
      display(??);
      slen -= leng;
  } while(slen>0);

}
void main(void)
{
split (pMsgString);
}

hm... i dun quite get what u mean... actually what i want to know is how i store the segmented message into a pointer so that when i call the function it would only display the segmented part.In simple terms, how can i declare a pointer and place it into the ?? part so that the display function will only display the segmented message?
Hopefully u ppl can understand my english becos i a not quite good in it.
0
 
bbtssAuthor Commented:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data .................................."};
void display(char *pMss)
{
 printf("%s",pMss);
 getch();
}
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();
      display(??);
      slen -= leng;
  } while(slen>0);

}
void main(void)
{
split (pMsgString);
}

hm... i dun quite get what u mean... actually what i want to know is how i store the segmented message into a pointer so that when i call the function it would only display the segmented part.In simple terms, how can i declare a pointer and place it into the ?? part so that the display function will only display the segmented message?
Hopefully u ppl can understand my english becos i a not quite good in it.
0
 
bbtssAuthor Commented:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data .................................."};
void display(char *pMss)
{
 printf("%s",pMss);
 getch();
}
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();
      display(??);
      slen -= leng;
  } while(slen>0);

}
void main(void)
{
split (pMsgString);
}

hm... i dun quite get what u mean... actually what i want to know is how i store the segmented message into a pointer so that when i call the function it would only display the segmented part.In simple terms, how can i declare a pointer and place it into the ?? part so that the display function will only display the segmented message?
Hopefully u ppl can understand my english becos i a not quite good in it.
0
 
bbtssAuthor Commented:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data .................................."};
void display(char *pMss)
{
 printf("%s",pMss);
 getch();
}
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();
      display(??);
      slen -= leng;
  } while(slen>0);

}
void main(void)
{
split (pMsgString);
}

hm... i dun quite get what u mean... actually what i want to know is how i store the segmented message into a pointer so that when i call the function it would only display the segmented part.In simple terms, how can i declare a pointer and place it into the ?? part so that the display function will only display the segmented message?
Hopefully u ppl can understand my english becos i a not quite good in it.
0
 
bbtssAuthor Commented:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data .................................."};
void display(char *pMss)
{
 printf("%s",pMss);
 getch();
}
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();
      display(??);
      slen -= leng;
  } while(slen>0);

}
void main(void)
{
split (pMsgString);
}

hm... i dun quite get what u mean... actually what i want to know is how i store the segmented message into a pointer so that when i call the function it would only display the segmented part.In simple terms, how can i declare a pointer and place it into the ?? part so that the display function will only display the segmented message?
Hopefully u ppl can understand my english becos i a not quite good in it.
0
 
bbtssAuthor Commented:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data .................................."};
void display(char *pMss)
{
 printf("%s",pMss);
 getch();
}
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();
      display(??);
      slen -= leng;
  } while(slen>0);

}
void main(void)
{
split (pMsgString);
}

hm... i dun quite get what u mean... actually what i want to know is how i store the segmented message into a pointer so that when i call the function it would only display the segmented part.In simple terms, how can i declare a pointer and place it into the ?? part so that the display function will only display the segmented message?
Hopefully u ppl can understand my english becos i a not quite good in it.
0
 
bbtssAuthor Commented:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data .................................."};
void display(char *pMss)
{
 printf("%s",pMss);
 getch();
}
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();
      display(??);
      slen -= leng;
  } while(slen>0);

}
void main(void)
{
split (pMsgString);
}

hm... i dun quite get what u mean... actually what i want to know is how i store the segmented message into a pointer so that when i call the function it would only display the segmented part.In simple terms, how can i declare a pointer and place it into the ?? part so that the display function will only display the segmented message?
Hopefully u ppl can understand my english becos i a not quite good in it.
0
 
bbtssAuthor Commented:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data .................................."};
void display(char *pMss)
{
 printf("%s",pMss);
 getch();
}
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();
      display(??);
      slen -= leng;
  } while(slen>0);

}
void main(void)
{
split (pMsgString);
}

hm... i dun quite get what u mean... actually what i want to know is how i store the segmented message into a pointer so that when i call the function it would only display the segmented part.In simple terms, how can i declare a pointer and place it into the ?? part so that the display function will only display the segmented message?
Hopefully u ppl can understand my english becos i a not quite good in it.
0
 
bbtssAuthor Commented:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data .................................."};
void display(char *pMss)
{
 printf("%s",pMss);
 getch();
}
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();
      display(??);
      slen -= leng;
  } while(slen>0);

}
void main(void)
{
split (pMsgString);
}

hm... i dun quite get what u mean... actually what i want to know is how i store the segmented message into a pointer so that when i call the function it would only display the segmented part.In simple terms, how can i declare a pointer and place it into the ?? part so that the display function will only display the segmented message?
Hopefully u ppl can understand my english becos i a not quite good in it.
0
 
bbtssAuthor Commented:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data .................................."};
void display(char *pMss)
{
 printf("%s",pMss);
 getch();
}
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();
      display(??);
      slen -= leng;
  } while(slen>0);

}
void main(void)
{
split (pMsgString);
}

hm... i dun quite get what u mean... actually what i want to know is how i store the segmented message into a pointer so that when i call the function it would only display the segmented part.In simple terms, how can i declare a pointer and place it into the ?? part so that the display function will only display the segmented message?
Hopefully u ppl can understand my english becos i a not quite good in it.
0
 
bbtssAuthor Commented:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data .................................."};
void display(char *pMss)
{
 printf("%s",pMss);
 getch();
}
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();
      display(??);
      slen -= leng;
  } while(slen>0);

}
void main(void)
{
split (pMsgString);
}

hm... i dun quite get what u mean... actually what i want to know is how i store the segmented message into a pointer so that when i call the function it would only display the segmented part.In simple terms, how can i declare a pointer and place it into the ?? part so that the display function will only display the segmented message?
Hopefully u ppl can understand my english becos i a not quite good in it.
0
 
bbtssAuthor Commented:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data .................................."};
void display(char *pMss)
{
 printf("%s",pMss);
 getch();
}
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();
      display(??);
      slen -= leng;
  } while(slen>0);

}
void main(void)
{
split (pMsgString);
}

hm... i dun quite get what u mean... actually what i want to know is how i store the segmented message into a pointer so that when i call the function it would only display the segmented part.In simple terms, how can i declare a pointer and place it into the ?? part so that the display function will only display the segmented message?
Hopefully u ppl can understand my english becos i a not quite good in it.
0
 
lowmakaCommented:
Don't worry, English is my second language, too and sorry to confuse you. I was trying to work into your programming logic. Have you tried "strtok"? Some example code for you, good luck to you.



/* strtok example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "This is a sample string,just testing.";
  char * pch;
  printf ("Splitting string \"%s\" in tokens:\n",str);
  pch = strtok (str, " ");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.");
  }
  return 0;
}
0
 
bbtssAuthor Commented:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data .................................."};
void display(char *pMss)
{
 printf("%s",pMss);
 getch();
}
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();
      display(??);
      slen -= leng;
  } while(slen>0);

}
void main(void)
{
split (pMsgString);
}

hm... i dun quite get what u mean... actually what i want to know is how i store the segmented message into a pointer so that when i call the function it would only display the segmented part.In simple terms, how can i declare a pointer and place it into the ?? part so that the display function will only display the segmented message?
Hopefully u ppl can understand my english becos i a not quite good in it.
0
 
bbtssAuthor Commented:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data .................................."};
void display(char *pMss)
{
 printf("%s",pMss);
 getch();
}
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();
      display(??);
      slen -= leng;
  } while(slen>0);

}
void main(void)
{
split (pMsgString);
}

hm... i dun quite get what u mean... actually what i want to know is how i store the segmented message into a pointer so that when i call the function it would only display the segmented part.In simple terms, how can i declare a pointer and place it into the ?? part so that the display function will only display the segmented message?
Hopefully u ppl can understand my english becos i a not quite good in it.
0
 
bbtssAuthor Commented:
Wow i didnt know i was pasting so much time...
well strtok seems to be only able to terminate at a null term. in my case i am suppose to send the characters 50 by 50. so i doubt i can use that.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> strtok seems to be only able to terminate at a null term

No, it'll terminate at whichever character you want. e.g., if you want it to terminate at a comma:

char str[] = "Eagles, Guns n' Roses, Aerosmith, Metallica" ;
char * p ;

p = strtok ( str, "," ) ;

while ( p != NULL )
{
  printf ( "%s\n", p ) ;
  p = strtok ( NULL, "," ) ;

} // end while

It'll print:

Eagles
<space>Guns n' Roses
<space>Aerosmith
<space>Metallica

Hope that helps!

Mayank.
 
0
 
Mayank SAssociate Director - Product EngineeringCommented:
If you want to store that segmented part in another 'pointer' (as you said), I'd say that you prefer to keep an array of strings and keep copying your segmented parts (substrings) as:

char str[] = "Eagles, Guns n' Roses, Aerosmith, Metallica" ;
char * p ;
char temp[10][80] ; // assuming 10 substrings
int count = 0 ;

p = strtok ( str, "," ) ;

while ( p != NULL )
{
  strcpy ( temp[count++], p ) ;
  p = strtok ( NULL, "," ) ;

} // end while

for ( int i = 0 ; i < count ; i ++ )
  printf ( "%s\n", temp[i] ) ; // end for


Hope that helps!

Mayank.
0
 
isprabuCommented:
You can try something like this:
Declare a local array variable inside the split function

     char buff[MAXLEN+1];

and then instead of printf use sprintf:

     sprintf(buff,"%*.*s\n", leng, leng, pF) ;
     display(buff);
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 16
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now