Thread: Check string for characters and concat characters to new string?

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    4

    Check string for characters and concat characters to new string?

    When using recursion to check a string for a certain character, how do I copy that character to another string?

    So I have something like:
    Code:
        for(i=0;s1[i]!='\0';i++)
        {
        if    (s1[i]=='x'|| 
             s1[i]=='y'|| 
             s1[i]=='z')
        {
        printf("\n%c",s1[i]);
        }
        }
    That'll print out each character that fits a condition in the string.

    I know that if I want to stick two strings together I can use strcat(s2,s1), but I can't use strcat(s2,s1[i]), or it will give me an error; "Invalid conversion from 'char' to 'const char*'".

    Can someone shed some light on this one for me?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Recursion does not appear to be used in your code snippet. Furthermore, it is badly in need of proper indentation, e.g.,
    Code:
    for (i = 0; s1[i] != '\0'; i++)
    {
        if (s1[i] == 'x'|| s1[i] == 'y'|| s1[i] == 'z')
        {
            printf("\n%c", s1[i]);
        }
    }
    Anyway, if you did use recursion, one approach would be to pass a pointer to (the first character of) the destination string as an argument. This way, the call that should do the copying writes to the destination string using this pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2016
    Posts
    4
    Thank you for trying to help, bear with me while I try to figure this out. I think I meant to say iteratively instead of recursively.

    I will try to indent it properly, thinking something like this:
    Code:
    for (i = 0; s1[i] != '\0'; i++)
    {
    	if (s1[i] == 'x' || s1[i] == 'y' || s1[i] == 'z')
    	{
    		ptr=&S1[i];
    		printf ("\n%c", *ptr); //This outputs the letter x/y/z if it occurs, so that's good
    		strcat(s2,*ptr);
    	}
    		printf ("\n%s",*ptr);//The output I want here is a new string of the previously found characters ex. "xyz" 
    }
    I'm still getting the same error. What am I not understanding?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you post the smallest and simplest program that you expect to compile, but which demonstrates the error.

    Based on the error message, my guess is that s2 is a char, so you should just assign to that char, but because I cannot see the declaration of s2, I cannot be certain.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2016
    Posts
    4
    Thanks for the reply. Here is a small version of what I'm trying to accomplish:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main(void)
    {
        char s1[50], s2[50], *ptr;
        int i;
        
        printf (" Enter String : \n");
        scanf (" %s ", s1);
        
        printf (" New string : \n");
        
        ptr = s1;
    
    
        for (i = 0; s1[i] != '\0'; i++)
        {
            if (s1[i] == 'x' || s1[i] == 'y' || s1[i] == 'z')
            {
                ptr = &s1[i];
                strcat(s2, *ptr);
            }
        printf (" %s \n", *ptr); //Just to be clear, lets say the user entered the string "abcxyz", the output would be "xyz".
        }
    }
    It gives me the error: (using dev-cpp if that means anything)
    [Error] invalid conversion from 'char' to 'const char*' [-fpermissive]
    [Note] initializing argument 2 of 'char* strcat(char*, const char*)'
    Last edited by racket; 11-08-2016 at 09:14 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    To append a single char using strcat, you have to make a small string out of it.
    Code:
    char temp[2] = { 0 };
    temp[0] = s1[i];
    strcat(s2, temp);
    Your code also needs to ensure that s2 is an empty string before calling strcat for the first time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Nov 2016
    Posts
    4
    Code:
    #include <stdio.h>
    #include <string.h>
     
     
    int main(void)
    {
        char s1[50], s2[50]={'\0'};
        char temp[50] = {'\0'};
        int i=0;
        printf ("Enter String:");
        gets(s1);
        for (i = 0; s1[i]!='\0'; i++)
        {
            if (s1[i] == 'x' || s1[i] == 'y' || s1[i] == 'z')
            {
                temp[0] = s1[i];
                strcat(s2, temp);
            }
        }
        printf ("%s", s2); 
    }
    Thanks, it works!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-08-2016, 12:53 PM
  2. Check to check string for a series of characters
    By robi in forum C Programming
    Replies: 1
    Last Post: 02-28-2012, 05:42 PM
  3. Replies: 11
    Last Post: 06-16-2011, 11:59 AM
  4. Replies: 5
    Last Post: 05-09-2010, 10:58 AM
  5. Concatenating: characters->string->vector (string) :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-02-2002, 01:14 PM

Tags for this Thread