Thread: 'C' question

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    13

    'C' question

    Hi all !
    Does anyone know how to generate all possible permutations for, lets say 5 / 6 letters of the alphabet( user is told to input a word), while converting to lowercase?
    Thanks !!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yep. Sure do.

    Now that we all know that I can do it, how about you post some of your code so we can see if you can do it.

    Quzah.

  3. #3
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    What Quzah means is try and have a go yourself, you'll learn a lot faster and if you still can't do it post what code you have and we'll tell you where you're going wrong.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    13
    ok.. first of all, i'm a newbie at C. I've got some sample code which did permutations for NUMBERS, not alphabets. I made some changes to it. There're many errors in the code listed below. Need to know what to do to make that change. Thanks !



    int n;
    int i;
    char letters; /* The permutation */

    void PrintPerm() {

    static const char *letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for (i=0; i <= n; i++)
    printf( "%c", a[i] );
    printf("\n");
    }

    void swap(char [i], char [j])
    {

    int temp;
    temp = a[i];
    a[i] = a[j];
    a[j] = temp;
    }

    int Next() {

    int k,j,r,s;

    k = n-1;
    while (a[k] > a[k+1]) k--;
    if (k == 0) return(0);
    else {
    j = n;
    while (a[k] > a[j]) j--;
    swap(j,k);
    r = n; s = k+1;
    while (r>s) {
    swap(r,s);
    r--; s++;
    }
    }
    PrintPerm();
    return(1);
    }

    void main () {

    int i;

    printf( "Enter a 5 / 6 letter word: " );
    scanf( "%c", &n );

    if (n<= i[0]) exit(1);

    printf( "\n" );
    for (i=[0]; i<=[n]; ++i) {
    a[i] = i;
    }
    PrintPerm();
    while (Next());
    printf( "\n" );
    }

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    13
    anyone??

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    one way of doing this is to use nested loops.
    i.e.

    for(char i='a';i<='z';i++)
    for(char j='a';j<='z';j++)
    for(char k='a';k<='z';k++)
    printf("%c%c%c\t",i,j,k);

    will do perms of 3 letters.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    13
    This is a very simplified version of the main body of my program. Can't get it to compile. Any thoughts? Pls reply as soon as you can. THANKS !!

    #include <stdio.h>
    #include <stdlib.h>

    main()
    {
    char first;
    char second;
    char third;
    char forth;
    char fifth;
    char alphabets;


    static constant char alphabets [25]= {'A','B','C','D','E','F','G','H','I','J','K','L',' M','N','O','P','Q','R','S','T','U','V','W','X','Y' ,'Z'};
    printf(" Enter a 5 letter word, with no spaces in between ");
    scanf("%c%c%c%c%c", &first,&second,&third,&forth,&fifth);
    {
    for(char first ='A';first<='Z'; first++)
    for(char second ='A';second<='Z'; second++)
    for(char third ='A';third<='Z'; third++)
    for(char third ='A';forth<='Z'; forth++)
    for(char third ='A';fifth<='Z'; fifth++)
    ;
    }

    printf("%c%c%c%c%c\t",first,second,third,forth,fif th);
    printf("\n");
    return 0;

    }

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    The way i showed you wont work for user input. In that case you can still use nested loops but the idea is to use them to cycle through the elements of the inputted string. i.e. for a five letter word to print all the chars in all poss orders then use five nested loops each counting from 0 to 4 then at the heart of the loops you need a printf statement like ....

    printf("%c%c%c%c%c\n",mystring[i],mystring[j],mystring[k],mystring[l],mystring[m]);

    You will probably be better using tabs instead of newlines to space with.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    13
    how would you define and initialize mystring ? This is the code I have so far, including mystring. Pls feel free to adjust the code
    Thanks

    #include <stdio.h>
    #include <stdlib.h>

    main()
    {
    char first;
    char second;
    char third;
    char forth;
    char fifth;
    char alphabets;


    static const char alphabets [27]= {'A','B','C','D','E','F','G','H','I','J','K','L',' M','N','O','P','Q','R','S','T','U','V','W','X','Y' ,'Z'};
    printf(" Enter a 5 letter word, with no spaces in between ");
    scanf("%c%c%c%c%c", &first,&second,&third,&forth,&fifth);
    {
    for(char first ='A';first<='Z'; first++)
    {
    printf("%c%c%c%c%c\n" ,mystring[first],mystring[second],mystring[third],mystring[forth],mystring[fifth]);
    }
    for(char second ='A';second<='Z'; second++)
    {
    printf("%c%c%c%c%c\n" ,mystring[first],mystring[second],mystring[third],mystring[forth],mystring[fifth]);
    }
    for(char third ='A';third<='Z'; third++)
    {
    printf("%c%c%c%c%c\n" ,mystring[first],mystring[second],mystring[third],mystring[forth],mystring[fifth]);
    }
    for(char third ='A';forth<='Z'; forth++)
    {
    printf("%c%c%c%c%c\n" ,mystring[first],mystring[second],mystring[third],mystring[forth],mystring[fifth]);
    }

    for(char third ='A';fifth<='Z'; fifth++)
    {
    printf("%c%c%c%c%c\n" ,mystring[first],mystring[second],mystring[third],mystring[forth],mystring[fifth]);
    }

    char first = tolower(first);
    char second = tolower(second);
    char third = tolower(third);
    char forth = tolower(forth);
    char fifth = tolower(fifth);
    ;
    }

    printf("%c%c%c%c%c\t",first,second,third,forth,fif th);
    printf("\n");
    return 0;

    }

  10. #10
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    #include<stdio.h>
    #include<string.h>
    
    char* newgets(char* buffer,int num)
    {
    int i;
    fgets(buffer,num,stdin);
    i=strlen(buffer)-1;
    if (buffer[i]=='\n') buffer[i]='\0';
    return buffer;
    }
    
    int main()
    {
    int i,j,k,l,m;
    char buffer [6];
    printf("Enter a five letter word ?");
    newgets(buffer,6);
    printf("\nYou entered %s\n",buffer);
    printf("Possible perms are :-  ");
    for(i=0;i<5;i++)
    for(j=0;j<5;j++)
    for(k=0;k<5;k++)
    for(l=0;l<5;l++)
    for(m=0;m<5;m++)
    printf("%c%c%c%c%c\t\t",buffer[i],buffer[j],buffer[k],buffer[l],buffer[m]);
    return 0;
    }
    Its really that simple!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    13
    thanks stoned. you helped a lot. But how do you convert all letters to lowercase?. I tied toLower(). didn't work. Pls advise

  12. #12
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Back to Lower case or vice versa

    well I am really a begginer to C but I do know how to do that,

    declare a variable
    int caps2lowercase='A'-'a';// or 'a'-'A'; for viceversa
    if you don't know how to use it, how to get the values from the string reply and I'll help you.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    variable = tolower( variable );

    You cannot do: variable = 'A'-'a'; and then: variable = 'a'-'A'; to convert back. It doesn't work that way.

    Quzah.

  14. #14
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    caps to lowercase

    no I know I am just saying in case he wantst to get the other one just subtitute the codes.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    13

    Re: caps to lowercase

    Originally posted by elchulo2002
    no I know I am just saying in case he wantst to get the other one just subtitute the codes.
    actually, its just to convert all letters, regardless of uppercase or lowercase, to LOWERCASE. Need something to identify when the use input is uppercase and subsequently convert it to lowercase. Will a while Loop work?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM