Thread: Determining number of times a character appears in an array

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    48

    Determining number of times a character appears in an array

    Below is my program. It works but then it gives a run time check failure- stack around the variable 'str2' was corrupted. I have no idea what the deal is. Thanks
    Code:
    #include <stdio.h>
    #include <string.h>
    int main (void)
    {
    	int k = 0;
    	int count = 0;
    	int n;
    	char *pt;
    	char *pt2;
    	char str[256];
    	char str2[1];
    	printf("Enter sentence. \n");
    	while((str[k]=getchar()) != '\n')
    		k++;
    	str[k]='\0';
    	n= k + 1;
    	pt=str;
    	printf("Enter character to find in sentence. \n");
    	scanf("%s", str2);
    	pt2=str2;
    
    	while((pt=strstr(pt, pt2)) != NULL)
    	{
    		count++;
    		pt++;
    	}
    	printf("Number of times %s occurs: %d \n",str2, count);
    	
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    may that was a little confusing, heres a newer version with comments except i moved my while loop into a function. so here it is
    Code:
    #include <stdio.h>
    #include <string.h>
    int main (void)
    {
    	/*Declare and intialize variables*/
    
    	int k = 0;
    	
    	int sum;
    	int n;
    	char *pt;
    	char *pt2;
    	char str[256];
    	char str2[1];
    	int occurance(char *pt, char *pt2);
    	
    	/*Get user input and assign to first string*/
    	printf("Enter sentence. \n");
    	while((str[k]=getchar()) != '\n')
    		k++;
    	str[k]='\0';
    	n= k + 1;
    	pt=str;
    	
    	/*Get from user the character to be counted 
    	  and put in 2nd array*/
    	printf("Enter character to find in sentence. \n");
    	scanf("&#37;s", str2);
    	pt2=str2;
    	sum = occurance(pt, pt2);
    	printf("Number of times %s occurs: %d \n",str2, sum);
    	
    return 0;
    }
    /*----------------------------------
    This function counts the number of times
    a specific character occurs */
    int occurance(char *pt, char *pt2)
    {
    	/*Initialize variables*/
    	int count = 0;
    	
    	/*Count number of times character occurs*/
    	while((pt=strstr(pt, pt2)) != NULL)
    	{
    		count++;
    		pt++;
    	}
    	return (count);
    }

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    str2 needs to have at least 2 characters in order to contain the terminating null char. So str2[2].
    Or you could make str2 a char instead of an array and use strchr instead of strstr.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    so where do i need to make changes in my program i tried on my own now it wont even work or crashes

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    One goal in your program should be to make it as simple and as clear, as possible. If the problem says to search for a char, then search for a char, not a string.

    Code:
    	/*Get from user the character to be counted 
    	  and put in 2nd array*/
    	printf("Enter character to find in sentence. \n");
    //change this to a char
    	scanf("%s", str2);
    
    //you don't need a ptr, at all
    	pt2=str2;
    	sum = occurance(pt, pt2);
    	printf("Number of times %s occurs: %d \n",str2, sum);
    	
    return 0;
    }
    /*----------------------------------
    This function counts the number of times
    a specific character occurs */
    int occurance(char *pt, char *pt2)  //no pointers are needed 
    //no need to build a piano
    {
    	/*Initialize variables*/
    	int count = 0;
    	
    	/*Count number of times character occurs*/
    //use strchr() instead
    	while((pt=strstr(pt, pt2)) != NULL)
    	{
    		count++;
    		pt++;
    	}
    	return (count);
    }
    And you should be good to sort the rest out.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    Heres what i have now after what i think fixing it the way you tell me to.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (void)
    {
    	/*Declare and intialize variables*/
    	int k = 0;
    	int sum;
    	int n;
    	char str[256];
    	char str2;
    	int occurance(char str, char str2);
    	
    	/*Get user input and assign to first string*/
    	printf("Enter sentence. \n");
    	while((str[k]=getchar()) != '\n')
    		k++;
    	str[k]='\0';
    	n= k + 1;
    		
    	/*Get from user the character to be counted 
    	  and put in 2nd array*/
    	printf("Enter character to find in sentence. \n");
    	scanf("%c", str2);
    	sum = occurance(str, str2);
    	printf("Number of times %c occurs: %d \n",str2, sum);
    	
    return 0;
    }
    /*----------------------------------
    This function counts the number of times
    a specific character occurs */
    int occurance(char str, char str2)
    {
    	/*Initialize variables*/
    	int count = 0;
    	
    	/*Count number of times character occurs*/
    	while((str=strchr(str, str2)) != NULL)
    	{
    		count++;
    		str++;
    	}
    	return (count);
    }
    Last edited by belkins; 11-11-2008 at 11:29 PM.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    and compiles fine but when i try to execute it i get a run time error saying str2 isnt being intialized??? does anyone know whats wrong

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Since you also get a warning about this line
    Code:
    scanf("%c", str2)
    I would look at it more carefully.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    ? a warning, i thought they werent a big deal. i mean i can do scanf_s but still? does scanf need to be something else?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Since when did scanf not take a pointer?

    Edit: And it's extremely rare for warnings to not be a big deal. Yeah, whatever_s is an ignorable warning, but it's the other warning on that line that I'm talking about.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    i mean its not even letting the user type in a variable to initialize str2?

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    ok im an idiot i forget the &str, but i modified it and it asks the user but then it crashes?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You shouldn't be able to pass str to the function occurance, since it expects a char in the first parameter, not a char array.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    ok so i modified it as
    Code:
    int occurance(char *str, char str2)
    and fixed it so now its not giving me that error but now its not returning the right number, if i need to repost what i have i can if you wanna take a look

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    like if the user types "The dog runs fast." and want to see how many times s occurs, it should return 2 but returns 0 instead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. REmoving a character from a character array
    By Bladactania in forum C Programming
    Replies: 3
    Last Post: 02-11-2009, 02:59 PM
  2. Random number array
    By matt_570 in forum C++ Programming
    Replies: 12
    Last Post: 11-13-2008, 04:44 PM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM