#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2

    Question Filtering elements in an array


    I am trying to create a program which would convert some elements in an array to some other element. Here is the code:
    Code:
    #include <stdio.h>
    #define MAX ((int) (sizeof(original) / sizeof(original[0])))
    
    int main(void)
    {
    	char original[] = {0};
    	int i;	
    	
    	printf("Enter message: ");
    	for (i = 0; i < MAX; i++) {
    		scanf("%c", &original[i]);
    		if (original[i] == '\n')
    			break;
    	}
    	
    	printf("\nIn B1FF-speak: ");	
    	for (i = 0; i < MAX; i++) {
    		if (original[i] == 'A' || original[i] == 'a')
    		original[i] = '4';
    		if (original[i] == 'B' || original[i] == 'b')
    		original[i] = '8';
    		if (original[i] == 'c')
    		original[i] = 'C';
    		if (original[i] == 'd')
    		original[i] = 'D';
    		if (original[i] == 'E' || original[i] == 'e')
    		original[i] = '3';
    		if (original[i] == 'f')
    		original[i] = 'F';
    		if (original[i] == 'g')
    		original[i] = 'G';
    		if (original[i] == 'h')
    		original[i] = 'H';
    		if (original[i] == 'I' || original[i] == 'i')
    		original[i] = '1';
    		if (original[i] == 'j')
    		original[i] = 'J';
    		if (original[i] == 'k')
    		original[i] = 'K';
    		if (original[i] == 'l')
    		original[i] = 'L';
    		if (original[i] == 'm')
    		original[i] = 'M';
    		if (original[i] == 'n')
    		original[i] = 'N';
    		if (original[i] == 'O' || original[i] == 'o')
    		original[i] = '0';
    		if (original[i] == 'p')
    		original[i] = 'P';
    		if (original[i] == 'q')
    		original[i] = 'Q';
    		if (original[i] == 'r')
    		original[i] = 'R';
    		if (original[i] == 'S' || original[i] == 's')
    		original[i] = '5';
    		if (original[i] == 't')
    		original[i] = 'T';
    		if (original[i] == 'u')
    		original[i] = 'U';
    		if (original[i] == 'v')
    		original[i] = 'V';
    		if (original[i] == 'w')
    		original[i] = 'W';
    		if (original[i] == 'x')
    		original[i] = 'X';
    		if (original[i] == 'y')
    		original[i] = 'Y';
    		if (original[i] == 'z')
    		original[i] = 'Z';
    	}
    	for (i = 0; i < MAX; i++) {	
    		printf("%c", original[i]);
    	}
    	printf("!!!!!!!!!!");
    	
    	return 0;	
    }
    Now let's provide "Hey dude, C is rilly cool" as an input. It's supposed to return "H3Y DUD3, C 15 R1LLY C00L!!!!!!!!!!". But it returns, "H!!!!!!!!!!". Now if I enter "A", it does return "4", which means there is only problem with the last loop:
    Code:
    for (i = 0; i < MAX; i++) {	
    		printf("%c", original[i]);
    	}
    What's wrong with it?
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    236
    Rep Power
    28
    Does it properly print content of "original" immediately after it is entered, i.e. before loop?

    By the way here are convenient functions "fgetc" and "fputc" which allow reading and writing of a single character, which is far more handy than printfing (and especially scanfing) them. I dare to recommend google or your book index for their usage.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Originally Posted by rodiongork
    Does it properly print content of "original" immediately after it is entered, i.e. before loop?

    By the way here are convenient functions "fgetc" and "fputc" which allow reading and writing of a single character, which is far more handy than printfing (and especially scanfing) them. I dare to recommend google or your book index for their usage.
    It just prints "H!!!!!!!!!!" when "Hey dude, C is rilly cool" is provided.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    236
    Rep Power
    28
    OMG, you are again using array of a single element only.

    As we discussed earlier, such code will not work. Until you get to pointers and memory allocation, use array of some predefined size.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Originally Posted by rodiongork
    OMG, you are again using array of a single element only.

    As we discussed earlier, such code will not work. Until you get to pointers and memory allocation, use array of some predefined size.
    But the book says to use
    Code:
    ((int) (sizeof(a) / sizeof(a[i])))
    to determine the array length. This is confusing! :confused: What do I do, what do I do? :confused:
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Moreover, this
    Code:
    ((int) (sizeof(a) / sizeof(a[i])))
    actually worked in some other codes I wrote.
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    236
    Rep Power
    28
    Please return to our previous thread and reread it.

    actually worked in some other codes I wrote.
    This expression is pretty correct. It really gives you number of elements of array.

    However, if you declare array as:
    Code:
    int a[] = {0};
    it would be array of size 1. You can printf the value of MAX to be sure. As I tried to explain, this will not create dynamically growing array as you perhaps hope.

    Also keep in mind that if you really have array of size 1 and will try to use elements beyond the end of this array, no error would be given. However, usually you will violate some other data stored in this place which eventually lead to crash in some mysterious way some time later.

    Comments on this post

    • arman.khandaker agrees
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Originally Posted by rodiongork
    Please return to our previous thread and reread it.



    This expression is pretty correct. It really gives you number of elements of array.

    However, if you declare array as:
    Code:
    int a[] = {0};
    it would be array of size 1. You can printf the value of MAX to be sure. As I tried to explain, this will not create dynamically growing array as you perhaps hope.

    Also keep in mind that if you really have array of size 1 and will try to use elements beyond the end of this array, no error would be given. However, usually you will violate some other data stored in this place which eventually lead to crash in some mysterious way some time later.
    Ahhh I think I have got it now! Thank you sir! :)
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Just to update you, I did it this way now:
    Code:
    #include <stdio.h>
    #define MAX ((int) (sizeof(original) / sizeof(original[0])))
    
    int main(void)
    {
    	char original[100];
    	int i;	
    	
    	printf("Enter message: ");
    	for (i = 0; i < 100; i++) {
    		scanf("%c", &original[i]);
    		if (original[i] == '\n')
    			break;
    	}
    	
    	printf("\nIn B1FF-speak: ");	
    	for (i = 0; i < MAX; i++) {
    		if (original[i] == 'A' || original[i] == 'a')
    		original[i] = '4';
    		if (original[i] == 'B' || original[i] == 'b')
    		original[i] = '8';
    		if (original[i] == 'c')
    		original[i] = 'C';
    		if (original[i] == 'd')
    		original[i] = 'D';
    		if (original[i] == 'E' || original[i] == 'e')
    		original[i] = '3';
    		if (original[i] == 'f')
    		original[i] = 'F';
    		if (original[i] == 'g')
    		original[i] = 'G';
    		if (original[i] == 'h')
    		original[i] = 'H';
    		if (original[i] == 'I' || original[i] == 'i')
    		original[i] = '1';
    		if (original[i] == 'j')
    		original[i] = 'J';
    		if (original[i] == 'k')
    		original[i] = 'K';
    		if (original[i] == 'l')
    		original[i] = 'L';
    		if (original[i] == 'm')
    		original[i] = 'M';
    		if (original[i] == 'n')
    		original[i] = 'N';
    		if (original[i] == 'O' || original[i] == 'o')
    		original[i] = '0';
    		if (original[i] == 'p')
    		original[i] = 'P';
    		if (original[i] == 'q')
    		original[i] = 'Q';
    		if (original[i] == 'r')
    		original[i] = 'R';
    		if (original[i] == 'S' || original[i] == 's')
    		original[i] = '5';
    		if (original[i] == 't')
    		original[i] = 'T';
    		if (original[i] == 'u')
    		original[i] = 'U';
    		if (original[i] == 'v')
    		original[i] = 'V';
    		if (original[i] == 'w')
    		original[i] = 'W';
    		if (original[i] == 'x')
    		original[i] = 'X';
    		if (original[i] == 'y')
    		original[i] = 'Y';
    		if (original[i] == 'z')
    		original[i] = 'Z';
    	}
    	for (i = 0; i < MAX; i++) {	
    		if (original[i] == '\n')
    		break;
    		printf("%c", original[i]);
    	}
    	printf("!!!!!!!!!!");
    	
    	return 0;	
    }
    And it works fine!
  18. #10
  19. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Generalize your MAX macro as

    #define DIM(A) (sizeof(A)/sizeof(*(A)))


    The instead of MAX use DIM(original)

    I see that you've also hard coded 100 within a loop. That also should be DIM(original) .
    [code]Code tags[/code] are essential for python code and Makefiles!
  20. #11
  21. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    You might also want to learn to use the else keyword:
    Code:
    if (original[i] == 'A' || original[i] == 'a')
        original[i] = '4';
    else if (original[i] == 'B' || original[i] == 'b')
        original[i] = '8';
    else if (original[i] == 'E' || original[i] == 'e')
        original[i] = '3';
    else if (original[i] == 'I' || original[i] == 'i')
        original[i] = '1';
    else if (original[i] == 'S' || original[i] == 's')
        original[i] = '5';
    else
        original[i] = toupper(original[i]);
    That way, you don't need to write code for every other letter of the alphabet, just the ones that need to be transformed to 81FF5P34K!!!

    Comments on this post

    • arman.khandaker agrees
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Originally Posted by Scorpions4ever
    You might also want to learn to use the else keyword:
    Code:
    if (original[i] == 'A' || original[i] == 'a')
        original[i] = '4';
    else if (original[i] == 'B' || original[i] == 'b')
        original[i] = '8';
    else if (original[i] == 'E' || original[i] == 'e')
        original[i] = '3';
    else if (original[i] == 'I' || original[i] == 'i')
        original[i] = '1';
    else if (original[i] == 'S' || original[i] == 's')
        original[i] = '5';
    else
        original[i] = toupper(original[i]);
    That way, you don't need to write code for every other letter of the alphabet, just the ones that need to be transformed to 81FF5P34K!!!
    ahhh that actually shortens up the code! Thanks for the advice! :)

IMN logo majestic logo threadwatch logo seochat tools logo