Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 06-10-2005, 05:54 PM   #1 (permalink)
MicroBlaze
Registered User
 
Join Date: Jun 2005
Posts: 2
MicroBlaze is on a distinguished road
Question Help Adding a CHAR to the end of a STRING

Code:
//====================================================

int main (void) {

   printf("------------------------------------------------\r\n");
   printf("-- Entering main() --\r\n");
   printf("------------------------------------------------\r\n");


  /* Help... after 3 hours I need some help...  I want to take a char
   and add it to the end of a string.  and then be able to print the 
   new string with a standard printf.  Here is a snip of the code I 
   can't get to work... */

// 16 char string that will later be sent to a 2x16 LCD display 
  char LCDString[16] = "Switch = "; 

  char dipSwitch1;
  char dipSwitch2;  
  dipSwitch1 = '6';
  dipSwitch2 = '3';
  printf("the value of the dipSwitches are %c and %c \r\n", dipSwitch1,dipSwitch2);
// This does print the text "the value of charOnes is 6"
// Now I want to take charOnes and add it to the end of the string LCDString
// strcat only seems to work when I has 2 text strings like the following
  char str[80];
  strcpy (str,"strings  ");
  strcat (str,"have been ");
  strcat (str,"concatenated.");
  printf("%s \r\n",str);
/* But what I want is something like strcat (LCDString, charOnes)... 
  but this does not work.  charOnes ends up as odd charachers 
  like "¨°0" or some other unprintable thing... and it seems to 
  change based on the number of lines in my code... so I think I'm 
  getting some pointer or address of what I really want... help  */
  char str2[16];
  strcpy (str2,"Switch =  ");
  strcat (str2,dipSwitch1);
  printf("%s \r\n",str2);
// I want the string to be "Switch = 6"
  strcat (str2,dipSwitch2);
  printf("%s \r\n",str2);
// now I want it to be I want the string to be "Switch = 63"

   printf("------------------------------------------------\r\n");
   print("-- Exiting main() --\r\n");
   printf("------------------------------------------------\r\n");
   return 0;
}

Last edited by redhead; 06-11-2005 at 06:04 AM. Reason: added [code][/code] tags
MicroBlaze is offline   Reply With Quote
Old 06-11-2005, 06:04 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,721
redhead is on a distinguished road
Look at sprintf() or snprintf() depending on what is supported on your system.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 06-11-2005, 06:37 AM   #3 (permalink)
MicroBlaze
Registered User
 
Join Date: Jun 2005
Posts: 2
MicroBlaze is on a distinguished road
is there a smaller way to do this? my system is an embedded system and I don't have much memory. I just tried sprintf() and it's too big. Is there a way to do this without using sprintf(). For example, I'm wrote a function hex2char(num) where I just do an if else and assign a char for 0 through F. Then I have another function that can take any number and split it into it's individual digits, where 243 would get changed to 2,4,3 then I change each to the char '2','4','3'. now I what to just stick the char '2','4','3' into a string 243. I need the do this with the smallest amount of compiled code... Any better ideas?
MicroBlaze is offline   Reply With Quote
Old 06-11-2005, 11:06 AM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,721
redhead is on a distinguished road
You need to be sure, theres room for the addition in the resulting char* if you just want to add it.
Something like this would do:
Code:
#include <stdio.h>
#include <string.h>
#define LENGTH 80
int main(){
    char str[LENGTH +1];
    strncpy(str, LENGTH, "Something = ");
    printf("%s\n", str);
    if(strlen(str) < (LENGTH -1))
    {  /* add a char */
        str[strlen(str)] = '3';
        str[strlen(str)] = '\0';
    }
    printf("%s\n", str);
    return 0;
}
But you need to be sure, that you've already assigned enough memory to your string, befor the addition, and that you NULL terminates the string, else your program will have uncontrolable behavior.
But the memory failure will only occur when you dont know what you're doing, you could easily free() the previusly used memory after your snprintf() and have that available instead of using alot and never free it.
ie:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LENGTH 80

int main(){
    char *str1, *str2;
    str1 = (char*)malloc(LENGTH*sizeof(char));
    str2 = (char*)malloc(LENGTH*sizeof(char));
    if(str1 != NULL)
        strncpy(str1, LENGTH, "Somethign = ");
    printf("%s\n", str1);
    if(str2 != NULL)
        if(strlen(str1) < LENGTH)
            if(sprintf(str2, "%s%c", str1, '3'))
              free(str1);
              /* memory previusly occupied by str1 
               * is now free to be used elsewhere */
    printf("%s\n", str2);
    free(str2);
    /* memory previusly occupied by str2 
     * is now free to be used elsewhere */
    return 0;
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting char to string? hyowza Java 1 03-25-2005 04:02 AM
Help for another program Androto Standard C, C++ 54 10-15-2004 07:21 AM
From C to Java HighterDK Java 11 07-13-2004 07:15 PM


All times are GMT -8. The time now is 03:11 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting