Computer Science Canada huge problem :/ |
Author: | bonne [ Tue Apr 02, 2013 9:42 am ] | ||
Post subject: | huge problem :/ | ||
Hi guys, I need help with a code that I'm trying do (I'm not a programmer). I need to open a txt file and read two characters (without quotes) example: "/ /" - "/ **" and "* /" and show to user the amount of each charactere. I have this code and i can tell to user the amount of @ in a txt file.
But i need do this with these characters: "/ /" - "/ **" and "* /" Please, help! |
Author: | DemonWasp [ Tue Apr 02, 2013 10:42 am ] |
Post subject: | RE:huge problem :/ |
What have you tried to do to convert the program you listed there into the program you want? What problems have you encountered? Please wrap your code in [ code ] tags next time. |
Author: | Cancer Sol [ Tue Apr 02, 2013 10:45 am ] |
Post subject: | Re: huge problem :/ |
bonne @ 4/2/2013, 9:42 am wrote: Hi guys, I need help with a code that I'm trying do (I'm not a programmer).
I need to open a txt file and read two characters (without quotes) example: "/ /" - "/ **" and "* /" and show to user the amount of each charactere. I have this code and i can tell to user the amount of @ in a txt file. #include <stdio.h> #include <stdlib.h> int main () { FILE * pFile; char * strstr ( const char *, const char * ); int c; int a= 0; pFile = fopen ("outro.txt", "r"); if (pFile !=NULL) { do { //DO c = fgetc(pFile); if (c=='@')a++; }//DO while (c != EOF); fclose(pFile); printf("The amount of @ = %d \n",a); } system("pause"); return 0; } But i need do this with these characters: "/ /" - "/ **" and "* /" Please, help! Umm it's kinda hard understanding what you're trying to say. // and /* */ are comments; the compiler skips it (I think) so the linker won't make it part of the program (it's probably hard for you to understand me since I don't even know what I'm saying sometimes ) |
Author: | bonne [ Tue Apr 02, 2013 11:01 am ] |
Post subject: | RE:huge problem :/ |
I need to access a txt file (outro.txt) and must recognize these characters in this file: / /, / ** and * / that will be with other characters and I need it to return the amount of each of these characters (/ /, / ** and * /) in the file outro.txt. Example outro.txt: / / A b c d e f / g * h i / / j k l m 8? 5 && * / |
Author: | nullptr [ Tue Apr 02, 2013 3:40 pm ] |
Post subject: | Re: RE:huge problem :/ |
bonne @ Tue Apr 02, 2013 11:01 am wrote: I need to access a txt file (outro.txt) and must recognize these characters in this file: / /, / ** and * / that will be with other characters and I need it to return the amount of each of these characters (/ /, / ** and * /) in the file outro.txt.
Example outro.txt: / / A b c d e f / g * h i / / j k l m 8? 5 && * / What should the program return with this input? |
Author: | bonne [ Wed Apr 03, 2013 2:15 pm ] |
Post subject: | Re: RE:huge problem :/ |
nullptr @ Tue Apr 02, 2013 8:40 pm wrote: bonne @ Tue Apr 02, 2013 11:01 am wrote: I need to access a txt file (outro.txt) and must recognize these characters in this file: / /, / ** and * / that will be with other characters and I need it to return the amount of each of these characters (/ /, / ** and * /) in the file outro.txt.
Example outro.txt: // A b c d e f / g * h i // j k l m 8? 5 && * / What should the program return with this input? I have this entries in "outro.txt": // A b c d e f /** h i // j k l m 8? 5 && * / // /** asdfggh // asasd I want the program show me the totals for: "//" , "/**" and "*/" in "outro.txt" I know i need to use substring but i don't know how to use... |
Author: | nullptr [ Wed Apr 03, 2013 5:20 pm ] |
Post subject: | Re: RE:huge problem :/ |
bonne @ Wed Apr 03, 2013 2:15 pm wrote: nullptr @ Tue Apr 02, 2013 8:40 pm wrote: bonne @ Tue Apr 02, 2013 11:01 am wrote: I need to access a txt file (outro.txt) and must recognize these characters in this file: / /, / ** and * / that will be with other characters and I need it to return the amount of each of these characters (/ /, / ** and * /) in the file outro.txt.
Example outro.txt: // A b c d e f / g * h i // j k l m 8? 5 && * / What should the program return with this input? I have this entries in "outro.txt": // A b c d e f /** h i // j k l m 8? 5 && * / // /** asdfggh // asasd I want the program show me the totals for: "//" , "/**" and "*/" in "outro.txt" I know i need to use substring but i don't know how to use... Suppose you complete your program and run it with this "outro.txt". What should the program output? You can do it using substring, but there are better ways available. Take a look at this document for some String functions that could be useful: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html |
Author: | Insectoid [ Wed Apr 03, 2013 6:18 pm ] |
Post subject: | RE:huge problem :/ |
Those are pretty nice string functions. Unfortunately, C strings don't do all that much. These functions might be more useful. |
Author: | bonne [ Thu Apr 04, 2013 8:45 am ] |
Post subject: | Re: RE:huge problem :/ |
Insectoid @ Wed Apr 03, 2013 11:18 pm wrote: Those are pretty nice string functions. Unfortunately, C strings don't do all that much. These functions might be more useful.
Hi Insectoid and nullptr, thanks a lot for your help but I so noob that i don't know how to use substring in my code. I know just basic things in C... Can some one teach me how to use substring in my code or make some example for "how to use" with external txt file, please? |
Author: | Cancer Sol [ Thu Apr 04, 2013 8:02 pm ] |
Post subject: | Re: RE:huge problem :/ |
bonne @ 4/4/2013, 8:45 am wrote: Insectoid @ Wed Apr 03, 2013 11:18 pm wrote: Those are pretty nice string functions. Unfortunately, C strings don't do all that much. These functions might be more useful.
Hi Insectoid and nullptr, thanks a lot for your help but I so noob that i don't know how to use substring in my code. I know just basic things in C... Can some one teach me how to use substring in my code or make some example for "how to use" with external txt file, please? Try using google. If you can't find any, then ask |
Author: | bl0ckeduser [ Fri Apr 05, 2013 8:52 am ] | ||||
Post subject: | Re: huge problem :/ | ||||
Here's a quick attempt at a C solution to your problem.
Quick test:
tries might be a bit overkill, i don't know. maybe you can use regexes or something. EDIT: fixed the code to deal with cases like "/////*****//" |
Author: | bonne [ Fri Apr 05, 2013 9:58 am ] |
Post subject: | Re: RE:huge problem :/ |
Cancer Sol @ Fri Apr 05, 2013 1:02 am wrote: bonne @ 4/4/2013, 8:45 am wrote: Insectoid @ Wed Apr 03, 2013 11:18 pm wrote: Those are pretty nice string functions. Unfortunately, C strings don't do all that much. These functions might be more useful.
Hi Insectoid and nullptr, thanks a lot for your help but I so noob that i don't know how to use substring in my code. I know just basic things in C... Can some one teach me how to use substring in my code or make some example for "how to use" with external txt file, please? Try using google. If you can't find any, then ask This is why I'm here. |
Author: | wtd [ Fri Apr 05, 2013 11:11 am ] | ||||||||||||||||||||||
Post subject: | Re: huge problem :/ | ||||||||||||||||||||||
Chars in C are simply 8-bit integers. As such, they can represent numbers between 0 and 255. These correspond to various visual characters in the ASCII character set. A pointer in C is either a 32 or 64-bit integer, depending on your system. For these purposes, the distinction is unimportant. A pointer stores the address in memory of some other piece of data. An array in C is several pieces of data of one type laid out sequentially in memory. It is represented in code as a pointer to the first element in the array. A string in C is simply an array of chars. By convention, strings in C are char arrays which end in the value zero. The presence of this zero tells string-handling functions that the string has ended. Let's consider looping over an array of characters:
This is pretty straightforward. The zero at the end of the string is added automatically by the compiler. We then loop over the string like any other array. Our condition of termination is just that the character not equal zero. If we want to print all of the two character sequences, we can do that. We just need to make sure the character after the current one isn't zero
Or perhaps we can express this using a loop to print the characters inside each two character section.
Now that we've done this, we can easily modify it for three characters.
Or, more generally...
Now, another problem we have to determine is if two strings are the same. To do this we have to loop over both and compare them one character at a time. We can cheat a bit by returning zero (false) from the function the first time we find a mismatch. If we make it to the end of the loop, we can assume the two are equal. [i]But, the loop might end because one string ended, but the other didn't. We'll test for this after the loop, since the variable i will still exist.
It would help to know the length of a string. We'll just loop until we find the end, and keep track of how many times we loop. Our loop doesn't actually have to do anything.
Let's revisit printing the substrings, but instead of just printing the characters, we'll build a new string representing the substring. I'll have to allocate enough memory to hold the substring using malloc. And then to have space for the zero, we'll add 1.
Now, using our ability to find equal strings, what if we want to avoid printing parts of a substring that matches a given string? So, if we have "Hello world!" and want to print three character substrings, but get the following. The substring to match would be "ell".
If the current substring matches the one we're looking for, we'll just skip the variable i ahead the length of the substring, but minus one, because the loop will do that part for us.
Now that we have that condition, how hard would it be to take out the printing and just count the number of matches?
Hopefully you now have a better idea of how string handling works in C, and how your problem can be solved. |
Author: | Cancer Sol [ Fri Apr 05, 2013 5:35 pm ] |
Post subject: | Re: RE:huge problem :/ |
bonne @ 4/5/2013, 9:58 am wrote: Cancer Sol @ Fri Apr 05, 2013 1:02 am wrote: bonne @ 4/4/2013, 8:45 am wrote: Insectoid @ Wed Apr 03, 2013 11:18 pm wrote: Those are pretty nice string functions. Unfortunately, C strings don't do all that much. These functions might be more useful.
Hi Insectoid and nullptr, thanks a lot for your help but I so noob that i don't know how to use substring in my code. I know just basic things in C... Can some one teach me how to use substring in my code or make some example for "how to use" with external txt file, please? Try using google. If you can't find any, then ask This is why I'm here. http://lmgtfy.com/?q=+How+to+use+substring+in+C Try that. |
Author: | Insectoid [ Fri Apr 05, 2013 6:44 pm ] |
Post subject: | RE:huge problem :/ |
Not this time, Cancer Sol. |
Author: | Cancer Sol [ Sun Apr 07, 2013 6:05 pm ] |
Post subject: | Re: huge problem :/ |
But mine works. Edit: Maybe try that stackoverflow link? Not sure what content it has though. |
Author: | wtd [ Sun Apr 07, 2013 11:37 pm ] |
Post subject: | Re: huge problem :/ |
Fewer huge images to prove |