return-type char

This is a discussion on return-type char within the C Programming forums, part of the General Programming Boards category; Could someone please illustrate a way to make a function that returns a string? I was just wanting to test ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    119

    return-type char

    Could someone please illustrate a way to make a function that returns a string?

    I was just wanting to test something simple below, a program that reads a string, and converts all the 's' to dollar signs. But I'm not sure how to make it return a string, and the program messes up when it runs and just exits out....


    Code:
    #include <stdio.h>    
    
    char* conv(char *str);
    
    int main()
    {
    
    
          char string[256];
    
          char mstring[256]; // modified string
    
          printf("Enter a string:");
    
          fgets(string,sizeof(string),stdin);
    
         *mstring = conv(string);
    
         printf("%s", mstring);
    
         getchar();
    
          return 0;
    }
    
    char* conv(char *str) {
    int lp;
    char newchar;
        for (lp = 0; lp < str; lp++) {
              if (str[lp] == 's') { newchar = '$'; str[lp] = newchar; }
              }
              return *str;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    Code:
    #include <stdio.h>
    
    void conv1 ( const char *str, char *newstr ) {
      int lp;
      char newchar;
      for (lp = 0; str[lp] != '\0' ; lp++) {
        if (str[lp] == 's') {
          newstr[lp] = '$';
        } else {
          newstr[lp] = str[lp];
        }
      }
      newstr[lp] = '\0';
    }
    
    char *conv2 ( char *str ) {
      int lp;
      char newchar;
      for (lp = 0; str[lp] != '\0' ; lp++) {
        if (str[lp] == 's') {
          str[lp] = '$';
        }
      }
      return str;
    }
    
    int main()
    {
      char string[256];
      char *pstr;
      char mstring[256]; // modified string
    
      printf("Enter a string:");
      fgets(string,sizeof(string),stdin);
    
      // this doesn't modify the original copy
      conv1 ( string, mstring );
      printf( "Old=%sNew=%s", string, mstring );
    
      // this does
      pstr = conv2 ( string );
      printf( "Old=%sNew=%s", string, pstr );
    
      getchar();
      return 0;
    }
    
    
    $ ./a.out
    Enter a string:this is a test
    Old=this is a test
    New=thi$ i$ a te$t
    Old=thi$ i$ a te$t
    New=thi$ i$ a te$t

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Thanks Salem, your always helpfull, you truly are a God of the C

    btw, things are rocking and rolling for me now, as I'm learning more, I've been creating my own functions, and doing more in C then I've ever been able to learn before, what an awesome language.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21