In the future, it would be better to just post your code so it can be referenced easily.
First of all, proper indentation is very important. Here is your code with better formatting:
Code:
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
{
char longestw, shortestw, temp,i;
int longest = 0, shortest=0;
while(temp=='\0')
{
scanf("%c",&temp);
if(strlen(temp)>longest)
{
longestw=temp;
longest=strlen(temp);
}
if(shortest==0)
{
shortest= strlen(temp);
shortestw=temp;
}
} // ???
else(strlen(temp)<shortest)
{
shortest=strlen(temp);
shortestw=temp;
}
}
for(i=0; i<strlen(words);++i)
{
printf("%s",words[i]);
if(i<strlen-1)
{
printf("%s,", word);
}
else
{
printf(".");
}
}
printf("the longest word entered was %s with %d characters", longestw, longest);
printf("the longest word entered was %s with %d characters", shortestw, shortest);
getch();
return 0;
}
Here's what I see so far:
- With better indentation, it's clear that you have an extra closing curly brace on line 24, which is an error.
- There's no reason to use "conio.h" - it is non-standard, and you're only using it for the "getch()" at the end of the program. This can accomplished easily with standard library functions.
- "main()" return "int", not "void"
- You need to use character arrays to store strings. You only have single character variables declared. This is insufficient to hold a
string (which is a character array terminated with a null character, '\0').
- The variable "temp" is uninitialized, and is used (in the condition of the "while()" loop) before it is ever given a value.
- You're only reading a single character with "scanf()". You need to read a string. "scanf()" with "%s" will stop reading at the first whitespace, so this is only good for a single word. "
fgets()" is probably better for this project.
- You're using variables that are never declared ("words[]", "word").
It seems clear that this code was written all at once without any intermediate testing (and perhaps with a hefty dose of copying). It is good practice to break you problem down into small steps, and tackle (and test) each one separately. And when you write code, compile often to make sure there are no mistakes along the way. Also, this is worth a read:
A development process
Also, a pencil and paper can go a long way in developing logic. Don't try to do it all in your head - figure it out by hand first, then use that to develop your code.
I would recommend writing a program to read a string from the user and print it out to the screen. Compile and test. When it's working, add the next thing bit of code (say, to determine the longest word). Build up the program in steps.