Thread: Here I go again...arrays....char...

  1. #16
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    Ok, this is the same thing. You can see that code I have written up is working, but now i want to do it this way...

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main()
    {
    	int sranje;
    	char niz[50];
    
    	printf("Rijec: ");
    	fgets(niz,sizeof(niz),stdin);
    
    	funk(niz);
    	scanf("%d", &sranje);
    }
    
    int funk(char niz[]){
    
    	char *niz2, *niz3;
    	int i,j=0,d=0;
    	niz2=malloc(sizeof(char));
    	niz3=malloc(sizeof(char));
    
    	for(i=0;i<strlen(niz)+1;i++){
    
    		if(niz[i]>=65 && niz[i]<= 122){
    
    			if(niz[i]=='A' || niz[i]=='E' || niz[i]=='I' || niz[i]=='O' || niz[i]=='U' ||
    				niz[i]=='a' || niz[i]=='e' || niz[i]=='i' || niz[i]=='o' || niz[i]=='u'){
    				
    				niz2[j]=niz[i];
    				j++;}
    			else
    				niz3[d]=niz[i];
    				d++;
    				}
    			}
    	printf("%s", niz2);
    	printf("\n%s", niz3);
    }
    It's the same thing but little shorter. Everything works, well not really, if i input "Zadar", output is " aa""" ", instead of " is 2, and for niz3 is " z"d"r " and I want it to be aa, zdr. Any suggestions are most welcome.
    ...and aprentice shall become master...or not...

    "Never let your sense of moral prevent you from doing what is right!" Salvor Hardin, mayor of Terminus

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You may want to do
    Code:
    niz2[j] = 0;
    niz3[d] = 0;
    before you try to print your strings.

    I would probably do something like this:
    Code:
        switch(tolower(niz[i]) {
           case 'a':
           case 'e':
           case 'i':
           case 'o':
           case 'u':
               niz2[..] = ....;
               break;
           default:
               niz3[...] = ...;
               break;
           }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    Works fine for niz2[j], but niz3 is still messed up! What niz2[j]=0; does? And when I dont enter anything it still prints out some signs for niz3.
    ...and aprentice shall become master...or not...

    "Never let your sense of moral prevent you from doing what is right!" Salvor Hardin, mayor of Terminus

  4. #19
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by shardin View Post
    Works fine for niz2[j], but niz3 is still messed up! What niz2[j]=0; does? And when I dont enter anything it still prints out some signs for niz3.
    Strings need to be terminated with a NULL character, otherwise the printf routine doesn't know where to stop outputting strings. It can't know after all, how much memory you've allocated. If you don't have a NULL terminator, then printf will continue reading memory past the end of your allocated memory until it encounters the first NULL char. That could be three chars from the end, it could be at the end of that page file or there could even not be any NULLs in memory.
    So you should always set the last char in your string to NULL, by doing:
    Code:
    niz2[j] = 0;
    /* or */
    niz2[j] = '\0';
    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #20
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    Ok, can someone explain to me why this ...

    Code:
    for(i=0;i<strlen(niz)+1;i++){
    
    		if(niz[i]>=65 && niz[i]<= 122){
    
    			if(niz[i]=='A' || niz[i]=='E' || niz[i]=='I' || niz[i]=='O' || niz[i]=='U' ||
    				niz[i]=='a' || niz[i]=='e' || niz[i]=='i' || niz[i]=='o' || niz[i]=='u'){
    				
    				niz2[j]=niz[i];
    				j++;}
    			else
    				niz3[d]=niz[i];
    				d++;
    				}
    			}
    ...is not working, and this...

    Code:
    	for(i=0;i<strlen(niz)+1;i++){
    		if(niz[i]>=65 && niz[i]<= 122){
                          
    				if(niz[i]=='A' || niz[i]=='E' || niz[i]=='I' || niz[i]=='O' || niz[i]=='U' ||
    				niz[i]=='a' || niz[i]=='e' || niz[i]=='i' || niz[i]=='o' || niz[i]=='u')
                  
    				niz2[j++]=niz[i];
    			else
    		niz3[d++]=niz[i];
    				}
    			 }
    ...works perfectly! In the upper code niz3 is messed up. Only difference is in...

    Code:
    {
    				
    				niz2[j]=niz[i];
    				j++;}
    			else
    				niz3[d]=niz[i];
    				d++;
    				}
    ...and...

    Code:
    				niz2[j++]=niz[i];
    			else
    		niz3[d++]=niz[i];
    ...and aprentice shall become master...or not...

    "Never let your sense of moral prevent you from doing what is right!" Salvor Hardin, mayor of Terminus

  6. #21
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Only one of your strings is going to get a \0 written to the end of it.

    Rearrange the code, and you get a different kind of luck.

    > if(niz[i]>=65 && niz[i]<= 122)
    How about
    if(niz[i]>='A' && niz[i]<= 'z')
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In this:
    Code:
    for(i=0;i<strlen(niz)+1;i++){
    
    		if(niz[i]>=65 && niz[i]<= 122){
    
    			if(niz[i]=='A' || niz[i]=='E' || niz[i]=='I' || niz[i]=='O' || niz[i]=='U' ||
    				niz[i]=='a' || niz[i]=='e' || niz[i]=='i' || niz[i]=='o' || niz[i]=='u'){
    				
    				niz2[j]=niz[i];
    				j++;}
    			else
    				niz3[d]=niz[i];
    				d++;
    				}
    			}
    Check the braces - particularly on the else-branch. If you use Emacs, mark the region and do "M-x indent-region", or in MSVC under Edit->Advanced->Format Selection (again after marking the region).

    Always using braces on ALL parts of if/else/while statements avoids this problem.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #23
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    > if(niz[i]>=65 && niz[i]<= 122)
    How about
    if(niz[i]>='A' && niz[i]<= 'z')
    Or even
    Code:
    #include <ctype.h>
    
    if(isalpha(niz[i]))
    assuming that the intent of the code was to see if niz[i] was a lower or uppercase letter.

    tolower() might also be called for -- then you don't have to check for 'E' and 'e', 'I' and 'i'.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #24
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Always using braces on ALL parts of if/else/while statements avoids this problem.
    Sound advice indeed!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #25
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Also:
    Code:
    for(i=0;i<strlen(niz)+1;i++){
    Calculating strlen() is expensive, you might want to do it outside of the loop. And that's the same as
    Code:
    for(i=0;i<=strlen(niz);i++){
    which might be easier to read, or it might not.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #26
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    I'm not allowed to use string functions in exam! Only strlen!
    ...and aprentice shall become master...or not...

    "Never let your sense of moral prevent you from doing what is right!" Salvor Hardin, mayor of Terminus

  12. #27
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    No, the thing is in braces and 'else' statement! If I use niz[i]=niz2[a], i++, then I need to use braces, if I use niz[i++]=niz2[a], then I dont need braces and then affects 'else' statement, yes I just realized! It doesnt have anything with upper, lower...
    ...and aprentice shall become master...or not...

    "Never let your sense of moral prevent you from doing what is right!" Salvor Hardin, mayor of Terminus

  13. #28
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I'm not allowed to use string functions in exam! Only strlen!
    I wasn't suggesting that you use a different function. I was suggesting that you calculate strlen() before the loop and save the result in a variable.
    Code:
    size_t len = strlen(niz);
    for(i=0;i<len+1;i++){
    It doesnt have anything with upper, lower...
    Your code also matches [, ], and other characters that are between 'Z' and 'a'. Is that what you wanted? Probably not. And your code isn't even portable. It could break with different character sets.

    All in all, isalpha() is a very good idea.

    The tolower() suggestion would make your code a lot easier.
    Code:
    			if(niz[i]=='A' || niz[i]=='E' || niz[i]=='I' || niz[i]=='O' || niz[i]=='U' ||
    				niz[i]=='a' || niz[i]=='e' || niz[i]=='i' || niz[i]=='o' || niz[i]=='u'){
    ->
    Code:
    			char c = tolower(niz[i]);
    			if(niz[i]=='a' || niz[i]=='e' || niz[i]=='i' || niz[i]=='o' || niz[i]=='u'){
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #29
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    No, no, no....I think you are missing my point.

    No, the thing is in braces and 'else' statement! If I use niz[i]=niz2[a], i++, then I need to use braces, if I use niz[i++]=niz2[a], then I dont need braces and then affects 'else' statement, yes I just realized!
    Last edited by shardin; 09-26-2007 at 02:22 PM.
    ...and aprentice shall become master...or not...

    "Never let your sense of moral prevent you from doing what is right!" Salvor Hardin, mayor of Terminus

  15. #30
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I realize that you identified the problem as being a case of missing curly braces, and that you can resolve this by using a single, more concise statement.

    My suggestions are just that -- suggestions. They won't affect the compilation or execution (much) of your program, except to perhaps speed it up (in the strlen() case) or not count [ and ] etc as letters (in the isalpha() case). And the tolower() one makes the code shorter.

    If you're not allowed to use functions from <ctype.h>, then . . . oh well. That would be ridiculous.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  2. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM