Thread: Returning String and Char from a Function

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    9

    Returning String and Char from a Function

    The is my code which is working, however, I want to convert everything to function but having difficulties.

    Code:
    #include <stdio.h>#include <math.h>
    #include <stdlib.h>
    #define N 20
    
    
    int main (void)
    {
    	char v, u, str[N];
    
    
    	printf("insert the string element\n");
    	scanf("%s", str);
    
    
    	printf("insert a character from the element\n");
    	scanf(" %c", &v);
    
    
        printf("\n");
    
    
    	printf("insert a character which you want to swap with\n");
    	scanf(" %c", &u);
    
    
    	printf("\n");
    
    
        int i;
    	for (i=0; str[i]!='\0'; i++){
            if (str[i] == v){
                str[i] = u;
            }
            else if (str[i] == u){
               str[i] = v;
            }
            else
                continue;
    
    
    	}
    
    
    	printf("the new string is:\n");
    	puts(str);
    
    
    	return 0;
    
    
    
    
    }
    In this other code, I'm trying to implement function on the first line but can't do it.

    Code:
    #include <stdio.h>#include <math.h>
    #include <stdlib.h>
    #define N 20
    char* stringP(char text[], char*);
    
    
    int main (void)
    {
    	char v, u, str[N];
    
    
    	str = ("insert the string element\n");
    
    
    	printf("insert a character from the element\n");
    	scanf(" %c", &v);
    
    
        printf("\n");
    
    
    	printf("insert a character which you want to swap with\n");
    	scanf(" %c", &u);
    
    
    	printf("\n");
    
    
        int i;
    	for (i=0; str[i]!='\0'; i++){
            if (str[i] == v){
                str[i] = u;
            }
            else if (str[i] == u){
               str[i] = v;
            }
            else
                continue;
    
    
    	}
    
    
    	printf("the new string is:\n");
    	puts(str);
    
    
    	return 0;
    
    
    
    
    }
    
    
    char* stringP(char text[], char news[N])
    {
        do{
        printf("s", text);
        scanf("s", &news);
        }
        while (news < N);
    
    
        return news;
    }
     
    
    Please help. I want to have at least 4 functions in this code

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,248
    You should not have functions just for the sake of having functions. Rather, use functions to simplify repetitive code that should be reused instead, or use them to group sections of code and name them.

    In your case, I would indeed have four functions: the main function, a function to handle input, a function to handle processing, and a function to handle output.
    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
    Oct 2019
    Posts
    9
    Quote Originally Posted by laserlight View Post
    You should not have functions just for the sake of having functions. Rather, use functions to simplify repetitive code that should be reused instead, or use them to group sections of code and name them.

    In your case, I would indeed have four functions: the main function, a function to handle input, a function to handle processing, and a function to handle output.
    ok I understand but my problem is how to declare a function that accepts character and string and how to call it. I'm only familiar with integer. I'm still new to this

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,248
    Don't you have examples of that in your learning material? Your stringP function already does that, except that it is badly named and looks like it isn't correctly implemented.
    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
    Oct 2019
    Posts
    9
    My learning material has always been about integer functions. My function is not implemented well that is why I'm looking for help
    Quote Originally Posted by laserlight View Post
    Don't you have examples of that in your learning material? Your stringP function already does that, except that it is badly named and looks like it isn't correctly implemented.

  6. #6
    Registered User
    Join Date
    Aug 2019
    Location
    Inside a Singularity
    Posts
    177
    You need a change of learning material in that case. Try an online website or free, reliable PDF (there's a tonne of good material out there).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-03-2018, 10:53 AM
  2. returning long uint8_t char string
    By YCX in forum C Programming
    Replies: 1
    Last Post: 03-22-2015, 11:51 AM
  3. Replies: 5
    Last Post: 09-06-2011, 02:59 PM
  4. Returning string from char *function()
    By Skvr in forum C Programming
    Replies: 3
    Last Post: 07-26-2009, 12:36 PM
  5. Returning a string as a char*... Help!
    By smarttart62 in forum C Programming
    Replies: 25
    Last Post: 10-09-2006, 07:02 PM

Tags for this Thread