can anyone help me doing my simple assignment,please.
I feel fed up and got confused so far

Recommended Answers

All 11 Replies

Please post the code you have so far and the questions...

Please post the code you have so far and the questions...

The question is to ask the user to enter a string which might be a number,symbol,letter or mix of these.The string must be of 12 characters long and message should be dispayed to the user as follow:
If he enters less than 3 characters a message is'your string is too short please enter another string'
If he enter more than 12 charcter a message is 'your string is too long please enter another string'
Note: all entered characters must be from ASCII table

The question is to ask the user to enter a string which might be a number,symbol,letter or mix of these.The string must be of 12 characters long and message should be dispayed to the user as follow:
If he enters less than 3 characters a message is'your string is too short please enter another string'
If he enter more than 12 charcter a message is 'your string is too long please enter another string'
Note: all entered characters must be from ASCII table

For us to reply 'and stay within the forum rules' you must show some effort by posting what you have so far. So please post what you have so far.

Actually I am a very basic c user and still crowling at the beginning of the c programming and my simple effort was as follow:
#include <stdio.h>
main (){
int a=1;
printf("Please Enter Your String ...");
scanf("%d",&a);
for (a=1;a<13;a++)
printf("Your String Is Too Long\n");
printf("Please Re-enter your string\n");
for (a=1;a<3;a++)
printf("Your String Is Too Short\n");
printf("Please Re-enter your string\n");

}

Right away I would use the fgets function to get the string from the user and I would define your string as:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char**argv)
{
	char ch[30];
	fputs("enter some characters->", stdout);
	fgets(ch, 29, stdin);
	fprintf(stdout, "string->%s\n", ch);
	exit(EXIT_SUCCESS);
}

This hint should pretty much get you there

Actually, thinking it over, you probably want something closer to this

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char**argv)
{
	int ch, nc = 0;
	fputs("enter some characters->", stdout);
	
	while ((ch = fgetc(stdin)) != '\n')
	{
		++nc;
	}
	fprintf(stdout, "ans->%d\n", nc);
	exit(EXIT_SUCCESS);
}

Which just counts the number of characters entered

Actually, thinking it over, you probably want something closer to this

****

Which just counts the number of characters entered

I think you just did his/her homework. I think the homework would get F like fail, because of being made by someone else. If you would have a look on the code the person has provided then it is obvious, that the person has very little clue what was written. And what you have written, then that is made by skilled person. It is like doing a homework for firstgrader and by it using math function that are used on high school.

This is a very bad approach to teach or help someone to learn.

#include <stdio.h>
main ()
{
  int a=1;
  printf("Please Enter Your String      ...");
  scanf("%d",&a); 
  for (a=1;a<13;a++) printf("Your String Is Too Long\n");[/I]
  printf("Please Re-enter your string\n");
  for (a=1;a<3;a++)  printf("Your String Is Too Short\n");
  printf("Please Re-enter your string\n");
}

2: Something is missing there, like void for example?
5, 6: Reading a string and storing it to integer.
7: Just write 13 times the message about long string
8: Adding the string again? And there is no function to add/write it.
9: Just write 3 times the message about short string
10: Adding the string again? And there is no function to add/write it.

void main()
{
char str[80];
int length=0;
printf("enter the string");
gets(str);

for(int i=0;str[i]!='\0';i++);         // loop to calculate the characters of the string
length = i;          

if(length<3)
printf("the string is too short");
else if(length>12)
printf("the string is too long");
}

hope this is what you wanted.:)

void main()
{
char str[80];
int length=0;
printf("enter the string");
gets(str);

for(int i=0;str[i]!='\0';i++); // loop to calculate the characters of the string
length = i; 

if(length<3)
printf("the string is too short");
else if(length>12)
printf("the string is too long");
}

hope this is what you wanted. :)

or just...
There are some intention errors here to not to make it easy... :)

void main()
{
char str[80];
do{
printf("enter the string");
gets(name);
strlen(name);

if(strlen(name)<3)
   printf("the string is too short \n Please write again\n");
if(strlen(name)>12)
   printf("the string is too long \n Please write again\n");

)while (((strlen(name)<13)&&(strlen(name)>2))

}

Have a look here:

http://cpp-tutorial.cpp4u.com/C_style_string_functions.html

http://www.gnu.org/s/libc/manual/html_node/String-Length.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.