Thread: reducing string length if a space is read

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    43

    reducing string length if a space is read

    while using fgets to get the input from the user I am trying to reduce the string length of the array by one when a space is read.

    this is a small portion of the code.

    Code:
    while(fgets(a,255,stdin)){
            
            i= 0;
            if(a[i]==' '){
                length = strlen(a)-1;
                
            }
            else
            continue;
        }
    is this the right Idea?

    I tried it but now I don't get any output when I try and print array elements.

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    804
    You would need to show us a small working program. We don't know if or how 'a' was defined. ('a' is not a good name for an array to hold a string. "str", or something more descriptive would be better.)

    You don't "reduce the string length of the array" by simply subtracting 1 from the strlen() of the String. You are only reducing the strlen() if the first char in the array is a space. What about other spaces later in the same string?

    You need to explain better what you are trying to do.

  3. #3
    Registered User
    Join Date
    Apr 2018
    Posts
    43
    Code:
    #include <stdio.h>
    #include <string.h> 
    #include <stdlib.h>
    
      int main() {
    
        char array[20];
        int length = 0;
        int i;
        int key = 256;
    
        printf("input:  ");
    
        fgets(array, 20, stdin);
    
        length = strlen(array) - 1;
    
        key = key % length;
    
        if (key > 0) {
    
          for (i = 0; i < length; i++) {
    
            if (array[i] == ' ') {
    
              printf("%c", array[i]);
    
              continue;
    
            }
            array[i] = array[i] + key;
    
            printf("%c", array[i]);
          }
    
        }
    
        return 0;
      }
    so this code gets user input string
    then 256 mod input length will be calculated. if the result is 3 all letters will be shifted by three. however when the input is something like this "hey whatsup" the length should be 10 and not 11. the space get's counted aswell.

    I am trying to tell the program not to add spaces to the input length. But it should print out the space , which i managed to implement.

    I can't seem to figure out how to tell fgets to remove spaces from the length.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,312
    You're approaching this from the wrong angle: fgets doesn't count the string length such that strlen will return it; strlen counts the strlen and you cannot change it to skip counting spaces. Rather, use a loop and count this "string length but skip the spaces" yourself.

    Also, you cannot use this "string length but skip the spaces" in the loop where you print the characters. Furthermore, adding to the character could give you a result that is an unprintable character... are you trying to "shift" according to the alphabet instead, as in a Caesar cipher?
    Last edited by laserlight; 12-05-2018 at 10:39 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2018
    Posts
    43
    yeah that's what I am trying to do. shifting as in caesar cipher

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read string length when string contains the null character
    By DecoratorFawn82 in forum C Programming
    Replies: 4
    Last Post: 01-06-2018, 09:07 AM
  2. Reducing the length of code
    By Taka in forum C Programming
    Replies: 4
    Last Post: 10-17-2006, 11:15 AM
  3. doubt in C--(reducing the length of code)
    By nardneran in forum C Programming
    Replies: 44
    Last Post: 10-15-2006, 02:38 PM
  4. Reducing Code size from ridiculous length
    By DanFraser in forum C# Programming
    Replies: 10
    Last Post: 01-18-2005, 05:50 PM

Tags for this Thread