Thread: Concatenate strings in c

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    19

    Concatenate strings in c

    I am getting error"incompatible integer to pointer conversation..." and don't know how to fix this. In my code, user inters line like this (3+3*(55-52)) I need to separate number from the line so I can do other operation.here is my code




    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include<string.h>
    
    
    int main()
    {
        int i=0;
        int j=0;
        char s[50];
        printf(":");
        scanf("%s",s);
        char n[5];
        while(s[i]!='\n' || s[i]=='+' || s[i]=='-'|| s[i]=='*'||s[i]=='/'|| s[i]=='(' ||s[i]==')')
        {
            
                if(j==0)
                {
                    strcpy(n,s[i]);   //error
                }
                
                strcat(n,s[i]);       //error
            printf("%s",n);
    
    
            i++;
        
        }
            
        
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    The function strcat and strncat concatenate one string onto another. In another words, both parameters must be char *. To concatenate a single character onto a string, you will need to do it slightly differently. For example you could implement a small function which in turn calls strcat

    Code:
    void concat_c(char *restrict str, char c, size_t n)
    {
    	char c_str[2];
    	c_str[0] = c;
    	c_str[1] = '\0';
    	strncat(str, c_str, n);
    }	
    
    // put '!' at the end of "hello" and print the result
    int main()
    {
    	char str[100] = "hello";
    	char c = '!';
    	concat_c(str, c, 100);	
    	puts(str);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Concatenate strings to communicate to a client
    By David25 in forum Networking/Device Communication
    Replies: 1
    Last Post: 09-30-2012, 01:27 AM
  2. Concatenate two strings.
    By ModeSix in forum C Programming
    Replies: 21
    Last Post: 04-26-2011, 10:12 AM
  3. Replies: 2
    Last Post: 03-30-2009, 12:25 AM
  4. concatenate two strings! K&R problem
    By karanmitra in forum Linux Programming
    Replies: 2
    Last Post: 08-09-2005, 10:44 AM
  5. Concatenate chars and std::strings.
    By Hulag in forum C++ Programming
    Replies: 3
    Last Post: 06-29-2005, 08:20 AM