Hi, I am trying to read a string using the function below.
The problem I have is that I can't write space in my string.
So strings like : "name fullanme" are rejected
and returns only "name".

char *getstring(char *ret, int max)
{
     char *ppp, *qqq;
     int invalid_characters=0;


     //fgets(ret,max,stdin); //it does not work
       scanf("%s",ret);


     for(ppp = qqq = ret; *ppp; ++ppp){

	
         switch(*ppp){
            case '0':     case '1':     case '2':    case '3':     case '4':   
            case '5':     case '6':     case '7':    case '8':     case '9':
            case '\n':    case '\t':   case '.':     case ',':     case '!':     
            case '?':     case ';':    case '$':     case '@':     case '#':     
            case '%':     case '^':    case '(':     case ')':     case '[':     
            case ']':     case '{':    case '}':     case '&':     case '*': invalid_characters++; 
                      break;

            default: *qqq++ = *ppp; 
                      break;
         }
     }

     *qqq = 0;

return((qqq != ret) ? ret : NULL);
}

Could you help me please to correct this ?

Edited 6 Years Ago by bufospro: n/a

Try fgets, this is from my Linux manpages(help files)

char *fgets(char *s, int size, FILE *stream);

gets() reads a line from stdin into the buffer pointed to by s until
either a terminating newline or EOF, which it replaces with '\0'. No
check for buffer overrun is performed (see BUGS below).

I have read this question like 10 times trying to make sense of what you are saying, but I still don't understand.

> The problem I have is that I can't write space in my string.
> So strings like : "name fullanme" are rejected
> and returns only "name".

I can't decide which one of these two things you are asking:

1) The program is not letting me type a space character. So what is happening is that strings entered by the user like "name fullname" are being rejected and returning only "name" when it should return the whole string.

2) The program is not allowed to read a space in the string. What is supposed to happen is that strings written like this "name fullanme" are to be rejected
and are supposed to return only "name".

Which is it?

Check out this piece of code

int 
main()
{
        char arr[10];                    //Max i/p size is 10 ie 9 letter and \0
        char* p = NULL;

        printf("Enter your name \n");
        fgets(arr,10,stdin) ;            //stdin is standard input   

        // If the user enters 9 or less chars then the first 10 chars will definitely have a \n .Otherwise there wont be a \n in the first 10 chars 

        if((p = strchr(arr,'\n')) != NULL)        
                arr[p - arr] ='\0';
        else
                arr[9] = '\0';

        printf("Your name is %s\n",arr);

        return 0;
}

I have read this question like 10 times trying to make sense of what you are saying, but I still don't understand.

> The problem I have is that I can't write space in my string.
> So strings like : "name fullanme" are rejected
> and returns only "name".

I can't decide which one of these two things you are asking:

1) The program is not letting me type a space character. So what is happening is that strings entered by the user like "name fullname" are being rejected and returning only "name" when it should return the whole string.

2) The program is not allowed to read a space in the string. What is supposed to happen is that strings written like this "name fullanme" are to be rejected
and are supposed to return only "name".

Which is it?

I am sorry my english is bad :(
What am I asking is I make a program that reads one string.

If user types $%^&1234prote then the program I want to save prote
If user types first! #second@ then the program I want to save first second
If uses types first second third then I want to save first second third
and so on

Edited 6 Years Ago by bufospro: n/a

I tested this code just to be sure it works. This is a better version of what you were trying to do.

// This will modify the given string to be cleaned of special characters.
// It will return the given modified cleaned string.

char *CleanString(char *String) {
   char *clean    = String, // Pointer to the clean part of the string
        *original = String; // Pointer to the original part of the string

   // Go through each character in the original string
   for (;original[0]!='\0'; original++) {
      switch (original[0]) {
        // Rejected Characters
        case '0':  case '1':  case '2': case '3': case '4': 
        case '5':  case '6':  case '7': case '8': case '9':
        case '\n': case '\t': case '.': case ',': case '!':
        case '?':  case ';':  case '$': case '@': case '#':
        case '%':  case '^':  case '(': case ')': case '[':
        case ']':  case '{':  case '}': case '&': case '*': 
           continue;
        default: break;
      }
      // If you got this far, the character is accepted, so 
      // copy it to the clean part of the string. Then move
      // the pointer up one character.
      clean[0] = original[0];
      clean++;
   }

   // At the end you will have equal or less than the original
   // length of the string. So whatever it is, copy a null
   // character to signify the end of the newly cleaned string.
   clean[0] = '\0';
   return String;
}

Using switch statements like this is perfectly fine, especially when the list of rejected characters could change or needs to be easy to modify for the programmer. If only letters are allowed, rather than specific characters, then using an if with range testing 'a'->'z' and 'A'->'Z' and ' ' would probably be more efficient.

Comments
You have a penchant for wroting people's code for them. Please stop. We are not a coding repository. We help people write their own code.

I tested this code just to be sure it works. This is a better version of what you were trying to do.

// This will modify the given string to be cleaned of special characters.
// It will return the given modified cleaned string.

char *CleanString(char *String) {
   char *clean    = String, // Pointer to the clean part of the string
        *original = String; // Pointer to the original part of the string

   // Go through each character in the original string
   for (;original[0]!='\0'; original++) {
      switch (original[0]) {
        // Rejected Characters
        case '0':  case '1':  case '2': case '3': case '4': 
        case '5':  case '6':  case '7': case '8': case '9':
        case '\n': case '\t': case '.': case ',': case '!':
        case '?':  case ';':  case '$': case '@': case '#':
        case '%':  case '^':  case '(': case ')': case '[':
        case ']':  case '{':  case '}': case '&': case '*': 
           continue;
        default: break;
      }
      // If you got this far, the character is accepted, so 
      // copy it to the clean part of the string. Then move
      // the pointer up one character.
      clean[0] = original[0];
      clean++;
   }

   // At the end you will have equal or less than the original
   // length of the string. So whatever it is, copy a null
   // character to signify the end of the newly cleaned string.
   clean[0] = '\0';
   return String;
}

Using switch statements like this is perfectly fine, especially when the list of rejected characters could change or needs to be easy to modify for the programmer. If only letters are allowed, rather than specific characters, then using an if with range testing 'a'->'z' and 'A'->'Z' and ' ' would probably be more efficient.

Thanks a lot

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