Thread: IF Construct and Printing Strings

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    3

    Cool IF Construct and Printing Strings

    This code i made divided a user input into characters and non character. The problem im having is that if a mixed sentence is created, such as 32B, it will only print the 'char string' and not the 'non char string'. But when the sentence is just non characters like 32 it will print the 'non-char string'. So essentially if a mixed sentences is created both of the strings won't be created or printed.(This is only a function by the way).
    Code:
     
    Count_chars( char Input[])
    {
    int i;
     for(i = 0; Input[i]; i++)
     {
    	if((Input[i] <= 122) &&(Input[i] >= 97)){
    	printf("Char %c\n",Input[i]);
    	Char[i] = Input[i];
    	}
    	else{
    	printf("Non-Char %c \n",Input[i]);
    	NonChar[i] = Input[i];
    	}
    }
    	printf("%s Char String\n",Char);
    	printf("%s NonChar String\n",NonChar);
    return;
    }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    i would think its a problem with your non-zero index on anything after the first run of the loop

    .....
    Also a string is NULL terminated
    Last edited by africanwizz; 04-26-2014 at 04:58 AM. Reason: meh..

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    1. What is the return type of your function?

    2. Where is the definition of Char and NonChar?

    3. Do you want to insert each "Char" or "Non-Char" into a new buffer? If so then you need another index counter such as j
    Code:
    Char[j++] = Input[i];
    4. You should use isalpha to determine if a character is alphabetic rather than using magic numbers.

  4. #4
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    @Africanwizz - the null termination is implied in the for loop, you don't have to explicitly state array[I] != '\0' (sort of like with other loops how you don't have to state ==true, or ==1) .

    Also if it's globally declared (like I suspect this to be), it should be initialized to '\0', so you wouldn't have to append the null at the end of copy.

    @Zombie-

    You should specify a return type on the function.

    Also, I don't know if Input[] has already had the newlines taken out, if not you will be getting an extra mysterious nonchar counted.

    The problem of either nonchar or char is because I is the index of both. Lets say I put in hello123. The function correctly copies the "hello" into Char[], but by the time it is at the numbers "123", I is already 5, so it starts copying them to the 6th element of NonChar[]. You can fix this like C99 says, by adding another variable (which would start NonChar off at element 0, and copy from there.
    Last edited by Alpo; 04-26-2014 at 12:36 PM. Reason: Miscounted

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Alpo
    @Africanwizz - the null termination is implied in the for loop, you don't have to explicitly state array[I] != '\0' (sort of like with other loops how you don't have to state ==true, or ==1) .
    I think that you misinterpreted africanwizz's statement. africanwizz implicitly observed that Char and NonChar might not be null terminated since we are not shown code that proves that they are indeed null terminated. Therefore, it is correct to suspect that they might not be null terminated, hence the printf calls using %s to print them may result in undefined behaviour.

    Quote Originally Posted by Alpo
    Also if it's globally declared automatic (like I suspect this to be), it should be initialized to '\0', so you wouldn't have to append the null at the end of copy.
    I think you meant to say that they are declared at file scope such that it has static storage duration. Indeed, if that were so and if the arrays are not initialised otherwise, then assuming no buffer overflow, the result will be a null terminated string. But this is not shown, and even so it is bad practice to write the code in this way, i.e., the unnecessary use of global variables and the assumption of null termination.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-12-2014, 05:46 PM
  2. Help printing strings
    By Frendin in forum C Programming
    Replies: 6
    Last Post: 12-09-2012, 07:39 PM
  3. printing out strings
    By mouse666666 in forum C Programming
    Replies: 2
    Last Post: 02-08-2010, 12:27 AM
  4. printing strings
    By Tom_Arch in forum C Programming
    Replies: 4
    Last Post: 04-25-2009, 03:23 AM
  5. printing char* strings
    By difficult.name in forum C Programming
    Replies: 4
    Last Post: 12-10-2004, 07:06 PM