‎08-11-2011 08:38 AM
Hi, two questions. Has anyone created a message popup that creates a justified format, or one that automatically inserts newlines? and, how do you use a pointer to copy a string from a beginning point to an end point. That is, if my string is 100 characters long, how can I copy from character 25 to 50?
Here is the start of my function
void simpleMessage(char *title, char *message){
int maxChars=75;
char tempString[1000],tempString2[100];
char *ptr;
strncpy (tempString, message,maxChars);
for(i=0,j=0;i<((strlen(message)/maxChars)-1);i++,j++){
ptr=strcat (tempString, "\n");
//here I need to copy the string starting at the pointer location to tempString2
//...
ptr=strcat(tempString,tempString2);
}
MessagePopup(title,tempString);
return;
}
Something I've considered, but I'll get to it when I get to it, is that the above would cut words apart, so I'd need to insert the new lines in place of a space character some +/- 10 characters of the max char length of the message.
Solved! Go to Solution.
‎08-17-2011 09:20 AM
Here is my solution! 😄
void justifiedMessage(int maxChars,char *title, char *message){
int i=0,j=0,k=0;
char fullString[1000],tempString[1000];
char *ptr=NULL;
strcpy (tempString, message);
ptr=strtok(tempString," ");
strcpy(fullString,tempString);
ptr=strcat(fullString," ");
while((ptr=strtok(NULL," "))!=NULL){
i++;
ptr=strcat(fullString,ptr);
j=strlen(fullString);
j=j-k*maxChars;
if(j<=maxChars)
ptr=strcat(fullString," ");
else{
ptr=strcat(fullString,"\n");
k++;
}
}
MessagePopup(title,fullString);
return;
}
‎08-17-2011 07:51 PM
Hi TurboMetrologist,
I am glad to see you were able to resolve your first question! Regarding the pointer to copy a string from a designated beginning point to an end point, the following is some coding that will work to serve this purpose:
#include <formatio.h>
#include <ansi_c.h>
int main (int argc, char *argv[])
{
char str1[] = "National Instruments CVI/LabWindows";
char str2[40];
CopyString (str2, 0, str1, 4, 10);
printf("Source String %s \n",str1);
printf("String Copied with Copy String %s \n", str2);
getchar();
return 0;
}
In this code, the CopyString function is used to start copying from the 4th character for a total of 10 bytes. Good luck with your code and I hope you have a great day!
Regards,
Jackie
‎08-18-2011 07:30 AM
Hey, thanks, that was more obvious than I was thinking.
I used this solution to grab between two instances of a delimiter.
char *getBetween(char *string,int firstInstance,int secondInstance,char *marker){
int i=0,j=0,k=0;
static char tempString[100];
char *ptr=NULL;
ptr=strtok(string,marker);
while((ptr=strtok(NULL,marker))!=NULL){
i++;
if(i>=secondInstance)
break;
else if (i==firstInstance){
strcpy(tempString,ptr);
}
else if (i>firstInstance){
strcat(tempString,marker);
strcat(tempString,ptr);
}
}
return tempString;
}
‎08-18-2011 08:23 AM
Hi TurboMetrologist,
Glad to hear you have been successful in your coding ventures! There are many ways to do this and if you have found a solution that works for you, then I think you are good to go! Have a great day and good luck with your project!
Regards,
Jackie