Thread: Substitute string with file

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    19

    Substitute string with file

    I wrote the following code:
    Code:
    #include "stdafx.h"
    #include "string.h"
    #include "ctype.h"
    
    int count_nonspace(const char* str)
    {
        int count = 0;
        while (*str)
        {
            if (!isspace(*str++))
                count++;
        }
        return count;
    }
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        int a[127];
        int i = 0, j = 0, count[127] = { 0 };
    
        int cur_count = 1; /* Gets compared with value in count[] */
        char cur_char = '\0';
        char string[100] = "Hellooooo world";
        for (i = 0; i < strlen(string); i++)
        {
            if (cur_char == string[i])
            {
                cur_count++;
            }
            else
            {
                if (32 < cur_char && cur_char < 127)
                {
                    if (cur_count > count[cur_char])
                    {
                        count[cur_char] = cur_count;
                    }
                }
                cur_char = string[i];
                cur_count = 1;
                if (32 < cur_char && cur_char < 127)
                {
                    if (!(count[cur_char]))
                    {
                        count[cur_char] = cur_count;
                    }
                }
            }
        }
    
        /* Find the most consecutive char and print it. */
        char max_char = '\0';
        int max_count = 0;
        for (j = 0; j < 127; j++)
        {
            if (max_count < count[j])
            {
                max_count = count[j];
                max_char = j;
            }
        }
        printf("%c\n", max_char);
    }
    This program is checking how many times each character appears in a row but it uses string as a source of a text and i want to use a file. I am not very fluent in operating on files, thus my question is what should be change in this program to make it work on files, not only on strings?

  2. #2
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Just FYI, you always post code that I can't just cut and paste to a file and compile. You doing so makes it highly unlikely that I will even look at your code.

    I have already passed on all other posts you have made.

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    19
    For me it is working, at least at visual studio 2013 it is working.
    Maybe if for some people my program is not working, you can provide me with a general idea how to substitute that?
    Last edited by mmk1234; 01-20-2015 at 05:04 PM.

  4. #4
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    WoodSTokk more of less did in the other thread you created and abandoned.

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    718
    Quote Originally Posted by mmk1234 View Post
    For me it is working, at least at visual studio 2013 it is working.
    Maybe if for some people my program is not working, you can provide me with a general idea how to substitute that?
    The code is written for Visual Stdio, but cannot compile under gcc on Linux. "_TCHAR" and "_tmain" are not standard C. Specific to Mickey$oft, as is "stdafx.h".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 05-05-2014, 07:39 AM
  2. Read file line, search for string, get string position
    By taifun in forum C++ Programming
    Replies: 15
    Last Post: 03-24-2014, 02:55 PM
  3. input asteriks to substitute characters
    By eastgod in forum C Programming
    Replies: 2
    Last Post: 12-10-2012, 08:37 AM
  4. any substitute for conio.h(turbo c++) in g++ compiler?
    By aditya1 in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2005, 04:10 PM
  5. Problem comparing string from text file with string constant
    By XenoCodex Admin in forum C++ Programming
    Replies: 3
    Last Post: 07-25-2002, 10:17 AM

Tags for this Thread