problem: char string[j] in if statement

This is a discussion on problem: char string[j] in if statement within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <string.h> void main() { char string[200]="int main() { int x=10; int y=5; if (x==10) { printf('hello'); ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    3

    problem: char string[j] in if statement

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void main()
    {
    
    	
    	char string[200]="int main() { int x=10; int y=5; if (x==10) { printf('hello'); } else {;} }";
    		
    	int count;
    	count = strlen(string);
    	printf("%d\n",count);
    
    	int parentheses = 0;
    	
    	for(int j=0; j<count; j++){
    		if(string[j] == 'x'){
    		parentheses++;
    		}	
                    }
    
    	printf("%d",parentheses);
    
    	
    }
    the lenght of string[200] is 74, variable parentheses is used to determine how many 'x' in string, but "if(string[j] == 'x')" always true, parentheses is 74 after a for loop finish, can some one correct me?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The output of this code is correct, there are

    2

    so what is the problem?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    One thing that scares me about that is the
    Code:
    for (int j = 0; j < count ; j++)
    but that is because I don't like the C99 (c++ type) declarations. . . though I don' t think that is the problem. Are you sure that you have an == where you have it up there?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Maybe you wanted to be counting parenthesis and not 'x'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  2. Replies: 6
    Last Post: 06-30-2005, 08:03 AM
  3. Problem with char and char*
    By chris1985 in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 11:44 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 10:55 AM
  5. String Processing Problem
    By muffin in forum C Programming
    Replies: 0
    Last Post: 08-24-2001, 10:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21