strings

This is a discussion on strings within the C Programming forums, part of the General Programming Boards category; Im trying to make a few basic functions for string manipulation. I get errors when I try to return a ...

  1. #1
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51

    strings

    Im trying to make a few basic functions for string manipulation.

    I get errors when I try to return a string from a function.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define COLS 80
    #define ROWS 80
    
    char right(char [], int);
    //char left(char [], int);
    
    int main() { 
      char string[ROWS];
      int n;
    
      printf("\nEnter in a string.");
      printf("\n:");
    
      gets(string);
      
      printf("\nEnter in a number to cut out of string.");
      printf("\n:");
      scanf("%d", &n);
    
      printf("\nString cut to right: %s", right(string, n));
      //printf("\nString cut to left: %s", left(string, n));
    
      return 0;
    }
    
    char right(char string[], int n) {
      char newString[ROWS];
      int ih, i = 0, j = 0;
    
      if((int)strlen(string) < n) {
        return string;  /* error here */
      }
      ih = strlen(string) - n;
    
      while(i<(int)strlen(string)) {
        if(i >= ih) {
          newString[j] = string[i];
          j++;
        }
        i++;
      }
      
      newString[j] = '\0';
    
      return newString;  /* error here */
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The reason you're getting an error is because you're returning a local variable, which is destroyed when the function goes out of scope.

    Change this:
    > char newString[ROWS];

    To these two lines:

    char newString[ROWS];
    char *s = NULL;


    Change this:

    > newString[j] = '\0';
    >
    > return newString; /* error here */

    To this:

    newString[j] = '\0';
    s = malloc( strlen( newString ) +1 );
    strncpy( s, newString, strlen( newString ) );
    return s;

    Now what you've just done is duplicated the contents in the buffer "newString", and put it in an allocated block of memory which you return via a pointer to it.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51

    strings

    still have problems.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define COLS 80
    #define ROWS 80
    
    char right(char [], int);
    //char left(char [], int);
    
    int main() { 
      char string[ROWS];
      int n;
    
      printf("\nEnter in a string.");
      printf("\n:");
    
      gets(string);
      
      printf("\nEnter in a number to cut out of string.");
      printf("\n:");
      scanf("%d", &n);
    
      printf("\nString cut to right: %s", right(string, n));
      //printf("\nString cut to left: %s", left(string, n));
    
      return 0;
    }
    
    char right(char string[], int n) {
      char newString[ROWS];
      char *s = NULL; 
      int ih, i = 0, j = 0;
    
      if((int)strlen(string) < n) {
        s = malloc(strlen(string)+1); 
        strncpy(s, string, strlen(string)); 
        return s;				/* error here */
      }
      ih = strlen(string) - n;
    
      while(i<(int)strlen(string)) {
        if(i >= ih) {
          newString[j] = string[i];
          j++;
        }
        i++;
      }
      
      newString[j] = '\0';
    
      s = malloc(strlen(newString)+1); 
      strncpy(s, newString, strlen(newString)); 
      return s;				/* error here */
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    char right(char string[], int n) {

    This should be a pointer to a character.

    char *right( ... )

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51

    strings

    Final form looked like this:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define COLS 80
    #define ROWS 80
    
    char *right(char [], int);
    char *left(char [], int);
    
    int main() { 
      char string[ROWS];
      int n;
    
      printf("\nEnter in a string.");
      printf("\n:");
    
      gets(string);
      
      printf("\nEnter in a number to cut out of string.");
      printf("\n:");
      scanf("%d", &n);
    
      printf("\nString cut to right: %s", right(string, n));
      printf("\nString cut to left: %s", left(string, n));
    
      return 0;
    }
    
    char *right(char string[], int n) {
      char *newString;
      int ih;
    
      if((int)strlen(string) < n) {
        return string;
      }
      ih = strlen(string) - n;
      newString = &(string[ih]);
    
      return newString;
    }
    
    char *left(char string[], int n) {
      char *newString;
    
      if((int)strlen(string) < n) {
        return string;
      }
      string[n] = '\0';
      newString = &(string[0]);
    
      return newString;
    }
    
    /* e.g.: othello is passed with a 5, othello size is 7
    ** 7 < 5 keep going
    ** ih = 7 - 5, ih = 2
    */
    Last edited by dharh; 02-05-2002 at 11:00 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 07:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 03:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 12:23 PM

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