Thread: Unidirectional cyclic list. Find most words whitch begin by letter A.

  1. #1
    Registered User NongMin's Avatar
    Join Date
    May 2011
    Posts
    10

    Unidirectional cyclic list. Find most words whitch begin by letter A.

    Expl1
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    struct sarasas
    {
        char duomuo;
        struct sarasas *kitas;
    };
    typedef struct sarasas Sarasas;
    typedef Sarasas *SarasasPtr;
    char i;
    int kiekis=0, kiekis_temp=0;
    int main()
    {
        SarasasPtr sar;
        sar=(SarasasPtr) malloc(sizeof(Sarasas));
        SarasasPtr pradzia=sar;
        //IVEDIMAS-------------------------------------------------------------------
        printf("Iveskite teksta (noredami nutraukti spauskite CTRL+Z):\n");
        printf("?");
        scanf("%c", &i);
        while(!feof(stdin))
        {
            sar->duomuo=i;
            sar->kitas=(SarasasPtr) malloc(sizeof(Sarasas));
            sar=sar->kitas;
            if(i=='\n')
                printf("?");
            scanf("%c", &i);
        }
        sar->kitas=pradzia;
        sar=pradzia;
        printf("\n\n");
        //SPAUSDINIMAS_IR_TIKRINIMAS--------------------------------------------------
    do
    {
    if((sar->duomuo==' ')||(sar->duomuo=='\n')) //TIKRINA
            if ((sar->kitas->duomuo=='A')||(sar->duomuo=='A'))
                kiekis_temp++;
            else
            {
                if (kiekis_temp>kiekis)
                {
                    kiekis=kiekis_temp;
                }
                kiekis_temp=0;
            }
    
        {
            printf("%c",sar->duomuo);       //SPAUSDINA
            sar=sar->kitas;
    
        }
    }while (sar->kitas!=pradzia);
    
        printf("\n\n");
        printf("Zodziu prasidedanciu raide A yra: %d", kiekis);   //ATSAKYMAS
        printf("\n");
    system("pause");
    return 0;
    }
    Expl2
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    struct sarasas
    {
        char duomuo;
        struct sarasas *kitas;
    };
    typedef struct sarasas Sarasas;
    typedef Sarasas *SarasasPtr;
    char i;
    int kiekis=0, kiekis_temp=0;
    int main()
    {
        SarasasPtr sar;
        sar=(SarasasPtr) malloc(sizeof(Sarasas));
        SarasasPtr pradzia=sar;
        SarasasPtr pabaiga=sar;
        //IVEDIMAS-------------------------------------------------------------------
        printf("Iveskite teksta (noredami nutraukti spauskite CTRL+Z):\n");
        printf("?");
        scanf("%c", &i);
        while(!feof(stdin))
        {
            sar->duomuo=i;
            sar->kitas=(SarasasPtr) malloc(sizeof(Sarasas));
            sar=sar->kitas;
            if(i=='\n')
                printf("?");
            scanf("%c", &i);
        }
        pabaiga=sar;
        sar->kitas=pradzia;
        sar=pradzia;
        printf("\n\n");
      //SPAUSDINIMAS_IR_TIKRINIMAS--------------------------------------------------
    do
    {
    if(sar->duomuo=='A')
        kiekis_temp++;
         if((sar->duomuo==' ')||(sar->duomuo=='\n')) //TIKRINA
            if ((sar->kitas->duomuo=='A')||(sar->duomuo=='A'))
                kiekis_temp++;
            else
            {
                if((sar->duomuo==' ')||(sar->duomuo=='\n')) 
                if ((sar->kitas->duomuo=='A')||(sar->duomuo=='A'))
                kiekis_temp++;
            }
            else
            {
                if (kiekis_temp>kiekis)
                {
                    kiekis=kiekis_temp;
                }
                kiekis_temp=0;
            }
    
        {
            printf("%c",sar->duomuo);       //SPAUSDINA
            sar=sar->kitas;
    
        }
    }while (pabaiga->kitas!=pradzia);
    
        printf("\n\n");
        printf("Zodziu prasidedanciu raide A yra: %d", kiekis);   //ATSAKYMAS
        printf("\n");
    system("pause");
    return 0;
    }
    In first example program did not count first word if it begin from "A", but sometimes it did not count last word if it begin from "A". In second example i think to white with "begining" and "end".

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yes, very nice... and???

  3. #3
    Registered User NongMin's Avatar
    Join Date
    May 2011
    Posts
    10
    I need to find how much words begin with letter A. But there are some mistakes, so i need help

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Don't make us guess...

    What are the mistakes? Compile warnings? errors? Some part of the code doesn't work right? Which part, what does it do or not do?

  5. #5
    Registered User NongMin's Avatar
    Join Date
    May 2011
    Posts
    10
    In first example program did not count FIRST word if it begin from "A", but sometimes it did not count last word if it begin from "A", so it is with if something, i think. So i wrote a second example with (pradzia "begin") and (pabaiga "end"), that the last and first count didn't miss.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Well, it's hard to follow the logic of the code since I cannot interpret the non-English text (print statements, variable names, etc.)

    You have an irregular brace style for one of your ifs.
    Near:
    Code:
        {
            printf("%c",sar->duomuo);       //SPAUSDINA
            sar=sar->kitas;
    
        }
    Probably works, but not clear. I get a warning.
    Last edited by mike65535; 05-02-2011 at 12:09 PM.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mike65535 View Post
    Well, it's hard to follow the logic of the code since I cannot interpret the non-English text (print statements, variable names, etc.)

    You have an irregular brace style for your ifs.
    Code:
     printf("%c",sar->duomuo);
    Probably works, but not clear. I get a warning.
    It looks like he's taking keyboard input and placing each character in a separate struct in a linked list... as in wow!

    Me... I'd use fgets() into a nice big char array and then go at it with strtok() to burst out words and check the first letter...

  8. #8
    Registered User NongMin's Avatar
    Join Date
    May 2011
    Posts
    10
    I have not got any warnings :S i will try to take advantage from fgets() and strtok()
    Last edited by NongMin; 05-02-2011 at 12:19 PM.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    20
    maybe we could do it without functions?

  10. #10
    Registered User
    Join Date
    Nov 2010
    Posts
    20
    Quote Originally Posted by CommonTater View Post
    It looks like he's taking keyboard input and placing each character in a separate struct in a linked list... as in wow!

    Me... I'd use fgets() into a nice big char array and then go at it with strtok() to burst out words and check the first letter...
    Man it's Unidirectional cyclic list u can't use fgets it's with files

  11. #11
    Registered User NongMin's Avatar
    Join Date
    May 2011
    Posts
    10
    I also whant to say that fgets is using just work with files.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You can use fgets to retrieve data from the keyboard. When you run your program three standard files are opened automatically, stdout for output to the screen, stdin for input from the keyboard, and stderr an unbuffered output file. So since you want to get data from the screen you would use stdin as the file.
    Code:
    fgets(YourVariable, YourVariable size, stdin);
    Jim

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I don't understand this:
    Quote Originally Posted by alionas View Post
    Man it's Unidirectional cyclic list u can't use fgets it's with files
    (are you saying fgets() doesn't work with files?)
    or this:
    Quote Originally Posted by NongMin
    I also whant to say that fgets is using just work with files.
    Can we try some actual English? With like, you know, punctuation or something?
    If you understand what you're doing, you're not learning anything.

  14. #14
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Find most words whitch begin by letter A.
    Why are you reading each letter into the linked list? If you were putting each word into the list, the algorithm (not the implementation mind) would make more sense.

    I'd use fgets() into a nice big char array and then go at it with strtok() to burst out words and check the first letter...
    That's better than the letter based linked list, but not by much.

    Man it's Unidirectional cyclic list
    So? Would storing each letter make more sense if it was bidirectional?

    Soma

  15. #15
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    (are you saying fgets() doesn't work with files?)
    I think it's being suggested that the OP use fgets, but fgets is for dealing with files. Seems clear, based on the use of scanf, that the OP wants the data coming in from the keyboard.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. list::begin() && list::end() question
    By nimitzhunter in forum C++ Programming
    Replies: 3
    Last Post: 02-01-2011, 10:46 AM
  2. Find letter
    By trentboyette in forum C++ Programming
    Replies: 3
    Last Post: 04-05-2010, 03:27 PM
  3. Replies: 1
    Last Post: 02-21-2010, 01:09 PM
  4. Replies: 7
    Last Post: 12-10-2007, 01:32 PM
  5. creating a wait funktion whitch will....
    By Shogun in forum C Programming
    Replies: 3
    Last Post: 04-11-2003, 02:35 PM

Tags for this Thread