"sizeof" character strings - output issue

This is a discussion on "sizeof" character strings - output issue within the C Programming forums, part of the General Programming Boards category; I'm trying to print the sizeof character strings. I understand that I get output of 15 because that is the ...

  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
    21,602
    Use strlen() from <string.h> instead of sizeof.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,488
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    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, 01: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, 09:46 PM

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