Hi,
I want to make a program that reads a string.
But I would like to read only characters, neither numbers nor special characters(@,#$%^&*()!). Also, no spaces between characters.

#include <stdio.h>
#include <string.h>

char *enter_a_string(int maxcharacters)
{

	char *ptr;
	int len;

	printf("Enter string : ");
	char array[50];

	do{
		scanf("%s",array);
	  	len = strlen(array);
			
		if(len > maxcharacters)
		{
			printf("Maximum %d  characters. Try again : ",maxcharacters);
		}

	}while(len > maxcharacters);

	ptr = array;

return ptr;
}

int main()
{
	char array[50];
	strcpy(array,get_personal_elements(15));
}

Could someone help me please?
Thanks a lot

Edited 7 Years Ago by bufospro: n/a

I want to make a program that reads a string.
But I would like to read only characters, neither numbers nor special characters(@,#$%^&*()!). Also, no spaces between characters.

Use system-specific APIs and write a fair amount of character-handling code.
Or use simple standard library functions and reject unwanted input.
Which is it that you want?

Use system-specific APIs and write a fair amount of character-handling code.
Or use simple standard library functions and reject unwanted input.
Which is it that you want?

Which of them is easier to understand ?
I am sorry, I don't understand good English, the only I think
is to be right and executable my code so as an user to insert his name without problems.

For example name='chris_%^&_ df' to be rejected.

may be this could help you.

char name[30];
int i;
printf("Enter name (only alphabets)");
scanf("%s",name);
for(i=0;name[i] != '\0' ; i++)
{
          if( !isalpha(name[i]) ) /* checking for non alphabet */
          { 
                 printf("not a proper name");
                 exit(0);
           }
}
printf("proper name %s",name);

The function isalpha() returns non-zero if its argument is a letter of the alphabet. Otherwise, zero is returned.
any modifications are welcome.

may be this could help you.

char name[30];
int i;
printf("Enter name (only alphabets)");
scanf("%s",name);
for(i=0;name[i] != '\0' ; i++)
{
          if( !isalpha(name[i]) ) /* checking for non alphabet */
          { 
                 printf("not a proper name");
                 exit(0);
           }
}
printf("proper name %s",name);

The function isalpha() returns non-zero if its argument is a letter of the alphabet. Otherwise, zero is returned.
any modifications are welcome.

thanks a lot

This article has been dead for over six months. Start a new discussion instead.