0

I have a simple program in mind where it reads the input first name and last name respectively and shows the number of characters present in each, but I am getting both the values as 5.

Here is the code:

#include<stdio.h>
void main()
{
    char fname[20];
    char lname[20];
    int count=0;
    int counter=0;
    printf("enter your  first name");
    scanf("%s",fname);
    printf("enter your last name");
    scanf("%s",lname);

    while((fname[count]>0) && (lname[counter]>0))
    { 
        count++;
        counter++;
    }
    printf("the no of char in ur fname  are %d ",count);
    printf(" the no of char in ur lname are %d ",counter);
}

Any advice would be of great help.

6
  • 3
    Note that 'void main()' is at best non-standard and is typically wrong. You should probably specify how long the name arrays in the scanf() formats ("%19s") to prevent overlong names causing crashes. You should print a newline at the end of the output (probably at the end of each of the last two printf() statements. If you are using a C99 compiler, omitting return 0; from the end of the program is 'OK' (he says with gritted teeth), but it is better to be explicit and return the correct status. You should probably make the two printf() statements more symmetric (no stray blanks). Commented Nov 23, 2010 at 6:03
  • thanks for helping would truly love to implement your suggestions and a very thanks again Commented Nov 23, 2010 at 6:09
  • @Jonathan Leffler - it's not against the standard if the implementation specifies it as a supported main signature. From the C99 standard (5.1.2.2.1): "It shall be defined with a return type of int and with no parameters ... or with two parameters ... or in some other implementation-defined manner" Commented Nov 23, 2010 at 6:14
  • @detly: yes - that's basically what I said. The standard says the return type is "int" and allows for implementation-defined alternatives which are not standard. Hence the caveat 'and is typically wrong'. Commented Nov 23, 2010 at 6:20
  • @detly: Microsoft says: 'Alternatively, the main and wmain functions can be declared as returning void (no return value). If you declare main or wmain as returning void, you cannot return an exit code to the parent process or operating system by using a return statement. To return an exit code when main or wmain is declared as void, you must use the exit function.' It is not clear to me what happens (what exit code is returned to the parent or o/s) when a program with void main() does exit - and the MS web site is silent too. Commented Nov 23, 2010 at 6:30

3 Answers 3

5

You're counting both in the same loop. Split it into two loops.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your help really helpful so i went ahead and made two loops of counter such as
2

As has already been pointed out, you are incrementing both counters at the same time - you need to split the counting into two separate loops (one for each string)

Try this instead:

while(fname[count] > 0)
{ 
    fnameCount++;
}

while(lname[count] > 0)
{ 
    lnameCount++;
}

printf("the no of characters in your first name is %d ", fnameCount);
printf("the no of characters in your last name is %d ", lnameCount);

1 Comment

hey thanks for reply to my question and your suggestion really worked thanks again
0

The problem is that your variables count and counter both end at the same time and will have the same value. Your if condition says to increment the two counters as long as either of the two char arrays have not yet ended. The moment you reach the end of one of the two char arrays, both counter variables are set to the length of the shorter array.

You need to have two for loops, one for each char array.

2 Comments

thanks for replyin but do i have to split the counter or the printf statements or both ..?
@jhonny: Read Kragen's answer to see what I meant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.