Thread: "sizeof" character strings - output issue

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    6

    "sizeof" character strings - output issue

    I'm trying to print the sizeof character strings. I understand that I get output of 15 because that is the size I indicated in my definitions. My question is, how can I make that value variable based on the input of first/last names?


    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    char * caps ( char * );
    
    
    void main ( void )
    {
    	char first[15];
    	char last[15];
    	int number;
    	char * p1;
    	char * p2;
    
    	printf("Enter your first name ");
    	gets(first);
    	printf("Enter your last name ");
    	gets(last);
    	printf("The original names were %s and %s\n",first,last);
    	p1 = first;
    	p2 = last;
    	caps(p1);
    	caps(p2);
    	printf("The capitalized names are %s and %s\n",first,last);
    
    	printf("Your first name, has %d letters\n", sizeof (first));
    
    }
    char* caps ( char *in )
    {
    	char * x;
    	x = in;
    	while (*x)
    	{
    		*x = toupper(*x);
    		x++;
    	}
    	return(in);
    }

    Thanks, Bunko

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use strlen() from <string.h> instead of sizeof.
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Not to mention reading the FAQs on "void main" and why "gets() is bad"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. copying strings to heap character arrays
    By SkyRaign in forum C++ Programming
    Replies: 4
    Last Post: 11-26-2006, 02:08 PM
  2. Character strings
    By campermama in forum C++ Programming
    Replies: 1
    Last Post: 06-17-2004, 04:26 PM
  3. Changing the character output.
    By InFeStEd-ArCh0n in forum C++ Programming
    Replies: 7
    Last Post: 05-03-2002, 12:39 PM
  4. Slow character output...
    By |<4D4\/eR in forum C++ Programming
    Replies: 4
    Last Post: 04-16-2002, 09:10 AM
  5. How do I output one character??
    By kia_1998 in forum C++ Programming
    Replies: 1
    Last Post: 10-29-2001, 10:46 PM