Thread: trouble joining strings

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    2

    trouble joining strings

    Hi, I am having trouble joining strings together and wonder if anyone can help. At the moment my strings are joining as string1 string2 string3 (ie with spaces between) and i want them to join as string1string2string3. Also when I output the final string before the 1's and 0's are printed I get a few random characters----is there a way to get rid of these? Many thanks for all suggestions

    Code:
    //Convert whole string to binary
      for (i=0; i<messages; i++)
      {
      dec2bin(e_array[i], t_array_bin);
      printf("\nBinary message of %d is %s\n", e_array[i], t_array_bin); 
      strcat(e_array_bin, t_array_bin);
      }
      printf("\nThe binary message is %s", e_array_bin);
      getchar();
      store_message(e_array_bin);    //put in file

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    When half the symbols in the code you present aren't shown to us, it's hard to figure out what exactly is going on. Try posting the whole thing.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    2
    ok, heres the whole code, apologies that it is quite messy
    (Dec2bin is a function that converts a decimal value into a binary string)
    Also, this is schoolwork---its not for production! Any help appreciated


    Code:
    // Function gets the message to be encrypted (from a binary file)
    
    #include <stdio.h>                // Code to get message
    #include <math.h>
    #include <stdlib.h>
    #include <string.h>
    
    FILE *pfile = NULL;      //declare a file pointer for file input/output
    
    void getmsg (void)
    {
       char nbin[100];      //binary conversion of n
       char e_array_bin[500];  //binary store encrypted message
       char t_array_bin[500];  //temp binary store encrypted message
       char extra[50]="0";
     
       int msglength=0; // variable to store the length of the message
       int nlength=0; // variable to store the length of nbin
       int encryptlength=0;
       int blocklength=0;
       int messages=0;
       int emsg;
      char msg[500];
    
       int m_array[50]; //array to store blocks
       int e_array[500];
       char txt_array[500];
    
       int n=0;
       int m=1;
       int i=0;
       int j=0;
       int count=0;
       int start=0;
       int sum=0;
       int x=0;
       int num=0;
    
       char *filename4 = "\message.txt"; 
    
       //Get the message from the 'message' file
       pfile = fopen(filename4, "r");
    
    	if (pfile == NULL)
    		{
    		printf("Erorr creating file %s for reading. Terminating....", filename4);
       		getchar();
    		}
    
    	fscanf(pfile, "%s", &msg);
    	printf("\nValue retrieved: %s\n",msg);
       fclose(pfile);
       getchar();
    
       // gets the length of the message in bits
       msglength=strlen(msg)-1;
       printf("The length of the message is %d\n\n", msglength);
       getchar();
    
       n=get_n();  //get n from public key
       // Convert the value of n into a binary string
       dec2bin(n,nbin);
    
       // gets the length of n in bits
       nlength=strlen(nbin)-1;
       printf("The length of the n is %d bits\n\n", nlength);
       getchar();
    
       //gets the length of n-1 in bits
       encryptlength=nlength-1;
       printf("The no. of bits to be encrypted per time is %d", encryptlength);
       getchar();
    
       //Pad the zeros out when the message does not split up evenly
       while ((msglength/encryptlength)%2 != 0)
       {
       strcat(msg, extra);  // Adds a '0' to the end of the message
       msglength++;
       }
       printf("Message is now %s\n", msg);
    
       // Get the number of individual messages to be encrypted
       messages=msglength/encryptlength;
       printf("Messages to encrypt is %d", messages);
       getchar();
    
       count=encryptlength;    //count set to no of bits we need to sum for each block
       printf("The value of count is %d", count);
       getchar();
    
       x=encryptlength-1;
       for (i=0; i<messages; i++)
       {
        	for (j=start; j<count; j++)
          {
             if (msg[j] == '1')
             {
             num = pow(2,x);// Multiply by power of 2 relevant
             sum=sum+num;     //sum will be in decimal
             //printf("Sum = %d\n", sum);
             }
             x--;
          }
          printf("The message:%d is %d\n", m, sum );
          m++;
          m_array[i]=sum;
          //printf("The value to be put in the array is %d\n", sum);
          sum=0;
          x=encryptlength-1;
          start=count;
          count=count+encryptlength;
          getchar();
      }
      printf("Blocks to encrypt are as follows: ");
      for (i=0; i<messages; i++)
      {
      	printf("%d ", m_array[i]);
      }
      getchar();
    
      //Display the results of encrypting each block
      sum=0;
      for (i=0; i<messages; i++)
      {
      e_array[i]=encrypt(m_array[i]);
      printf("Encrypted value is: %d\n ", e_array[i]);
      getchar();
      }
    
      //Display encrypted message
      printf("Encrypted message:");
      for (i=0; i<messages; i++)
      {
      printf("%d", e_array[i]);
      }
    
    //Convert whole encrypted string to binary
      for (i=0; i<messages; i++)
      {
      	dec2bin(e_array[i], t_array_bin);
      	printf("\nBinary message of %d is %s\n", e_array[i], t_array_bin);
      	strcat(e_array_bin, t_array_bin);
      }
      printf("\nThe binary message is %s", e_array_bin);
      getchar();
      store_emessage(e_array_bin);    //put in file  */
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could just do it the easy way:
    Code:
    char buf[BUFSIZ] = {0};
    char buf1[] = "Hello";
    char buf2[] = "World!";
    
    strcat( buf, buf1 );
    strcat( buf, " " );
    strcat( buf, buf2 );
    
    printf("%s\n", buf );
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some Trouble - Switching with strings.
    By mintsmike in forum C++ Programming
    Replies: 5
    Last Post: 05-02-2009, 09:04 AM
  2. file saving: trouble with strings and strncpy()
    By psychopath in forum C++ Programming
    Replies: 10
    Last Post: 09-17-2005, 10:26 PM
  3. Joining strings.
    By DaveHope in forum C++ Programming
    Replies: 10
    Last Post: 06-21-2005, 08:15 AM
  4. Noob trouble: returning strings (pointers to char)
    By SgtMuffles in forum C Programming
    Replies: 4
    Last Post: 02-28-2005, 08:48 PM
  5. joining strings with pointers?
    By sworc66 in forum C Programming
    Replies: 6
    Last Post: 09-03-2002, 05:02 PM