Like Tree2Likes
  • 1 Post By memcpy
  • 1 Post By oogabooga

[HELP] My capitalization program is not working

This is a discussion on [HELP] My capitalization program is not working within the C Programming forums, part of the General Programming Boards category; The problem in my simple learning program is the, basically, it doesn't work and it prints out Segmentation fault (core ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Location
    Caloocan, Metro Manila, Philippines
    Posts
    17

    Unhappy [HELP] My capitalization program is not working

    The problem in my simple learning program is the, basically, it doesn't work and it prints out Segmentation fault (core dumped). I just want to know why it does that and what are the possible solutions.

    EDIT: That's not really the main problem. Even if I just assign a value to input, it still prints out the error.
    Code:
    #include <stdio.h>#include <string.h>
    
    
    char *cap(char *);
    
    
    int main(int argc, char *argv[]) {
    	char *input = "Hello World!";
    
    
    	printf("CAPITALIZED: ");
    	cap(input);
    
    
    	return 0;
    }
    
    
    char *cap(char *input) {
    	int i, n;
    
    
    	for(i = 0, n = strlen(input); i < n; i++) {
    		if(input[i] >= 'a' && input[i] <= 'z') {
    			input[i] -= ('a' - 'A');
    			printf("%c", input[i]);
    		}
    		printf("\n");
    	}
    }
    Last edited by Arbyn Acosta; 08-12-2012 at 08:53 AM.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    You're giving scanf the address of your pointer, and when the user inputs data, the stack will get overwritten until the input runs out and/or the program crashes.

    Also, you need to allocate memory for "input", because right now it doesn't point to any valid memory location.
    rags_to_riches likes this.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287
    I haven't tested it, but what size have the 'input' buffer? If is 0 bytes you shouldn't have to scanf anything into it no?

    Niara

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287
    Ups sorry memcpy, I haven't seen your post

  5. #5
    Registered User
    Join Date
    Aug 2012
    Location
    Caloocan, Metro Manila, Philippines
    Posts
    17
    That's not really the problem. I just wanted users to give me an input. But even if I substitute it with a constant value, the core still gets dumped...

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    How are you substituting it with a "constant value"? Post the code.

    Also, you should check out <ctype.h>, using the functions provided in this header will simplify your code, especially toupper().

  7. #7
    Registered User
    Join Date
    Aug 2012
    Location
    Caloocan, Metro Manila, Philippines
    Posts
    17
    Quote Originally Posted by memcpy View Post
    How are you substituting it with a "constant value"? Post the code.

    Also, you should check out <ctype.h>, using the functions provided in this header will simplify your code, especially toupper().
    I know those things but I want to learn how this things work and be able to recreate it with on my own way. The code is already updated in the 1st post

  8. #8
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    Stop guessing and read about strings

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    In your current code you are trying to modify a constant string.
    Instead of this:

    char *input = "Hello World!";

    try this

    char input[] = "Hello World!";
    Salem likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Capitalization
    By jacek in forum C Programming
    Replies: 1
    Last Post: 01-24-2010, 06:03 PM
  2. Program not working
    By HAssan in forum C Programming
    Replies: 5
    Last Post: 01-11-2009, 02:45 AM
  3. Standard for header file naming/capitalization?
    By skorman00 in forum C Programming
    Replies: 4
    Last Post: 04-21-2006, 03:38 AM
  4. capitalization trick
    By volk in forum C++ Programming
    Replies: 11
    Last Post: 04-05-2003, 07:12 PM
  5. Capitalization help
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-20-2001, 12:39 AM

Tags for this Thread


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