Thread: trim last character of a string

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    48

    trim last character of a string

    Code:
    #include <stdio.h>
    
    
    char* LastcharDel(char* name);
    
    int main ()
    {
    	// declarations
    	char name[20];
    
    	printf("Enter your name:\n");
    	scanf("%s", name);
    
    	// fucntion calls
    	LastcharDel(name);
    
    	printf(" your name without last character : %s\n\n", name);
    
    	return 0;
    }
    
    char* LastcharDel(char* name)
    {
    	int i = 0;
    	for(i =0; i< 20; i++)
    	{
    	if(i > 1)
        name[i- 1] = '\0';
    	}
    
    	return name;
    }
    how do i do this problem? the problem require to write a function that accepts a string (a pointer to a character) and deletes the last character by moving the null character one position to the left.

    Also write a main function that inputs your family name, calls this function, and then ourputs your family name after the call (it should be missing the last letter at this point). Copy your code and the output (as a comment at the bottom of the code) as run by entering your name.

    the code above is what i got so far

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your current function is "deleting" all but the first two characters. You need to test for 0, then when you find it, set the character before that to 0 (unless the zero you found was the very first character).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Firstly, since you don't intend to use the return value of LastcharDel at all it should not return anything at all. Make it "void LastcharDel( char* name)"

    Secondly, it currently removes all the letters except the first because you're setting every character except the first to '\0'.

    Your process should be this:
    *Find the end of the string. What index does it have?
    *Set that index minus one to be the new end of the string.
    Code:
    while(!asleep) {
       sheep++;
    }

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    Code:
    /*
    	Dang Vo
    	[email protected]
    	assign 6
    	This program remove the last character of a string by moving the NULL character 
    	to the left 1, finally it output the string as missing the last character.
    */
    
    #include <stdio.h>
    
    char* LastcharDel(char* name);
    
    int main ()
    {
    	// declarations
    	char name[20];
    
    	printf("Enter your name:\n");
    	scanf("%s", name);
    
    	// fucntion calls
    	LastcharDel(name);
    
    	printf(" your name without last character : %s\n\n", name);
    
    	return 0;
    }
    
    char* LastcharDel(char* name)
    {
    	int i = 0;
    	while(name[i] != '\0')
    	{
    		i++;
    		
    	}
    	name[i-1] = '\0';
    	return name;
    }
    this is my new code but it only prompt to enter the input data it doesn't output it without the last character what do i suppose to fix now ?
    Last edited by khoavo123; 02-27-2012 at 06:30 PM.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    What do you mean? Your code works fine for me. Here is what I get when I compile/run your code:
    Code:
    $ gcc -Wall -g -std=c99  trim.c   -o trim
    $ ./trim
    Enter your name:
    Smith
     your name without last character : Smit
    Does your console window disappear before you can read the output perhaps?

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    yes my console disappear before i can read the output

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    may be i need to use a system "pause"

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    i still have a question about this though

    can any one interpret this because it kinda confusing
    Also write a main function that inputs your family name, calls this function

    what does that mean?
    Last edited by khoavo123; 02-27-2012 at 06:57 PM.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Hooray for self-reliance!

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    yes my console disappear before i can read the output

    Read this link: FAQ > Stop my Windows Console from disappearing everytime I run my program? - Cprogramming.com.

    Quote Originally Posted by khoavo123 View Post
    i still have a question about this though

    can any one interpret this because it kinda confusing
    Also write a main function that inputs your family name, calls this function

    what does that mean?
    You already did this:
    write a main funciton
    Code:
    int main()
    {
        ...
        return 0;
    }
    that inputs your family name
    Code:
    printf("Enter your name:\n");
    scanf("%s", name);
    calls this (the delete last char) function
    Code:
    // fucntion calls
    LastcharDel(name);

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    46
    If all you want to do is get rid of the last character in the string, you don't need to have anything in a loop.
    Since this string is statically allocated, there is no guarantee that it will be filled by whatever the user fills
    This is the easiest way I could think of
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void lastCharDel (char *name);
    
    int main (void)
    {
        char name[20];
        
        printf("Enter your name:\n");
        scanf("%s", name);
        lastCharDel(name);
        printf("your name without last character : %s\n\n", name);
        system("pause");
        return 0;   
    }
    
    void lastCharDel (char *name)
    {
         int length;
         length = strlen(name);//Get length of string
         name[length - 1] = '\0';//Set next to last char to null
         return;
    }
    Hope this helps

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    jeanermand: your example does not account for a zero length string. Refer to post #4 for khoavo123's own attempt that looks closer to the mark.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 12-04-2010, 12:04 AM
  2. comparing character in a string to anothr character
    By merike in forum C Programming
    Replies: 5
    Last Post: 05-11-2007, 12:16 AM
  3. Trim A String
    By pittuck in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2003, 07:38 AM
  4. trim string function (code)
    By ipe in forum C Programming
    Replies: 9
    Last Post: 01-06-2003, 12:28 AM
  5. Replies: 3
    Last Post: 11-03-2002, 02:14 AM