473,385 Members | 1,267 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

help with counting substings in strings???

Im a newby to programing 2 months exp.. I am trying to write code in C to take a input string from the user and then the input substring. I wish to compare the substring to the string and count the number of times it apears and print this to the user. After many hours of frustration i have gotten this far any help or derection would be aprieciated
Expand|Select|Wrap|Line Numbers
  1. # include<stdio.h>  
  2. # include<math.h>
  3. # include<stdlib.h>
  4. # include<string.h>
  5.  
  6. int main(void)
  7.  
  8. {
  9. char s1[200], s2[200];
  10. char count;
  11. int count2;
  12. int run;
  13. int i;
  14. int j;
  15. int strstrcnt(const char *s1,const char *s2);
  16. char s[200];
  17.  
  18. printf("\nThis is a program to find certian occurances of words in a user inputed sentence\n");
  19. printf("\nIt will display the number of times the word is found.\n");
  20.  
  21. printf("\nTo run this program and input the word to find and the sentence to search hit 1,\n\n Hit 2 to exit.\n");
  22.  
  23. scanf("%d",&run);
  24.  
  25. fflush(stdin);
  26.  
  27. while (run==1)
  28.  
  29. {printf("\nPlease input the sentence you wish to hae searched.\n");
  30.  
  31.  gets(s1);
  32.  
  33.  printf("\nPlease enter the word or term you wish to have found in the sentence.\n");
  34.  
  35.  gets(s2);
  36.  
  37.  strstrcnt(s1,s2);
  38.  
  39.  printf("%d",s1[0]);
  40.  
  41.  
  42. }
  43.  
  44. return(0);}
  45.  
  46. int strstrcnt(const char *s1,const char *s2)
  47. {
  48. char * strstr(const char *s1, const char *s2);
  49. return (0);}
  50.  
I know gets() is generally bad and know i am missing some code to compleate this just hoping for some derection and clarification.
Mar 28 '08 #1
4 2020
Im a newby to programing 2 months exp.. I am trying to write code in C to take a input string from the user and then the input substring. I wish to compare the substring to the string and count the number of times it apears and print this to the user. After many hours of frustration i have gotten this far any help or derection would be aprieciated
Expand|Select|Wrap|Line Numbers
  1. # include<stdio.h>  
  2. # include<math.h>
  3. # include<stdlib.h>
  4. # include<string.h>
  5.  
  6. int main(void)
  7.  
  8. {
  9. char s1[200], s2[200];
  10. char count;
  11. int count2;
  12. int run;
  13. int i;
  14. int j;
  15. int strstrcnt(const char *s1,const char *s2);
  16. char s[200];
  17.  
  18. printf("\nThis is a program to find certian occurances of words in a user inputed sentence\n");
  19. printf("\nIt will display the number of times the word is found.\n");
  20.  
  21. printf("\nTo run this program and input the word to find and the sentence to search hit 1,\n\n Hit 2 to exit.\n");
  22.  
  23. scanf("%d",&run);
  24.  
  25. fflush(stdin);
  26.  
  27. while (run==1)
  28.  
  29. {printf("\nPlease input the sentence you wish to hae searched.\n");
  30.  
  31.  gets(s1);
  32.  
  33.  printf("\nPlease enter the word or term you wish to have found in the sentence.\n");
  34.  
  35.  gets(s2);
  36.  
  37.  strstrcnt(s1,s2);
  38.  
  39.  printf("%d",s1[0]);
  40.  
  41.  
  42. }
  43.  
  44. return(0);}
  45.  
  46. int strstrcnt(const char *s1,const char *s2)
  47. {
  48. char * strstr(const char *s1, const char *s2);
  49. return (0);}
  50.  
No help yet? can anyone tell me where the return value from the strstr goes and am i able to check to see if it is null or not by if statments? Further does the return value to the array need to be 0 or should it be a pointer to s1 or s2?
Mar 30 '08 #2
No help yet? can anyone tell me where the return value from the strstr goes and am i able to check to see if it is null or not by if statments? Further does the return value to the array need to be 0 or should it be a pointer to s1 or s2?
According to link, strstr returns a pointer to first occurrence of the search word in the given sentence. So you could test something like this:

Expand|Select|Wrap|Line Numbers
  1. char sentence[200], word[200]; 
  2. char* occurs_at; 
  3. int location; 
  4.  
  5. //code to get user inputs for: sentence and word, then:
  6.  
  7. occurs_at = strstr(sentence, word);   //a pointer (to somewhere in sentence)
  8. if(occurs_at)  //if true
  9.    location = occurs_at - sentence;   //because sentence[location] == *occurs_at;
  10. else  //strstr has returned a null in occurs_at
  11.    printf("\nNo match");
  12.  
  13.  
--
Mar 30 '08 #3
You can use the strstr function from the C library. You can also use scanf instead of gets() function. Please refer to any standard text book to know the syntax and usage of these functions.

Here is the code you may want to add

Expand|Select|Wrap|Line Numbers
  1. char *c1 = "Input the matching string"; /*first string entered by user */
  2. char *c2 = "matching string"; /* the string to be matched entered by user */
  3. char *result = strstr(c1, c2);
  4. if (result != NULL)  {
  5. printf("The substring is %s \n", result);
  6. } else {
  7. printf("Could not find the substring %s from %s \n", c2, c1);
  8. }
  9.  
I hope this helps.

Ambrish Kinariwala
Mar 31 '08 #4
Thanks for the help i have it figured out with the suggestions, below is a chunk of the code i needed to add, hope others others can learn from it.
Expand|Select|Wrap|Line Numbers
  1.    char *q;//pointer
  2.    int count=0; for (i=0;i<length;i++)
  3.          q[i]=subcap[i];
  4.       q=strstr(q+1,s2);
  5.    q=strstr(s1,s2);
  6.    while (q!= NULL)
  7.    {q=strstr(q+1,s2);
  8.        count++;}//counter
  9.  
Mar 31 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Srinath Avadhanula | last post by:
Hello, I am wondering if there is a way of counting graphemes (or glyphs) in python. For example, in the following string: u'\u0915\u093e\u0915' ( or equivalently, u"\N{DEVANAGARI LETTER...
4
by: Travers Naran | last post by:
Here's the basic idea. I have a dictionary of substrings (the substrings stored as keys). I have a list of strings. I want to find out, for each word in the dictionary, how many times the...
7
by: Sam Lowry | last post by:
Greetings. I am trying to do something which should elementary for Perl, but I have only been able to find bits and pieces on it. When I put the bits together they do not work. Maybe I am going...
8
by: pembed2003 | last post by:
Hi all, As an exercise, I am trying to come up with a function to count the length of a char* string using recursion. I came up with this: int count_length(char* s){ if(s == 0) return 0;...
5
by: Melissa Cowan | last post by:
I am using Access 2000. I have the Developer's handbook and got the code for the mulit select listbox from there. It sends the selected value to another listbox, lstselected. What I need to do is...
28
by: Bailey.W87 | last post by:
my professor give me this assignment. Sort the R's B's and W's in an array. for example, the user enter: R B W W B B R W W R R W R B W i need to swap the characters in the array and arrange it...
14
by: ranjmis | last post by:
Hi all, Below is the code wherein I am initializing double dimentional array inside main with string literals. Now I want to display the strings using a function call to which I just want to...
21
by: c | last post by:
Hi everybody. I'm working on converting a program wriiten on perl to C, and facing a problem with concatenate strings. Now here is a small program that descripe the problem, if you help me to...
3
by: nitric | last post by:
hey guys, i'm really stuck on this program. It's basically a survey and I have to ask people what drinks they like. 1-4, coffee tea oj and lemonade. i'm having trouble counting the TOTAL NUMBER...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.