473,399 Members | 3,302 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,399 software developers and data experts.

C - Unscrable Words

I've been trying to make a program that takes as input a scramble word,
gets the anscii value of that scrambled word, and numerically sorts it. And tries to find the same numerical order in the word list.

Heres the code:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. //sort the anscci value numerically
  4. void sort(int a[], int len) {
  5.         int j, i, t;
  6.         for(i=len-1; i>=0; i--) {
  7.                 for(j=1; j<=i; j++) {
  8.                         if(a[j-1] > a[j]) {
  9.                                 t = a[j-1];
  10.                                 a[j-1] = a[j];
  11.                                 a[j] = t;
  12.                         }
  13.                 }
  14.         }
  15. }
  16. //get the anscii value of the string.
  17. void getanc(int a[], char b[]) {
  18.         int i;
  19.         for(i=0; i<strlen(b); i++) {
  20.                 a[i] = (int)b[i];
  21.         }
  22. }
  23.  
  24. main(int argc, char *argv[]) {
  25.         char file[1500][11], input[11];
  26.         int anc1[1500][11], anc2[11];
  27.         int i, j, x;
  28.         FILE *word = fopen("wordlist.txt", "r");
  29.         for(i=0; i<1480; i++) {
  30.                 fgets(file[i], 11, word);
  31.                 getanc(anc1[i], file[i]);
  32.                 sort(anc1[i], strlen(file[i]));
  33.         }
  34.         strcpy(input, argv[1]);
  35.         getanc(anc2, input);
  36.         sort(anc2, strlen(input));
  37.         for(i=0; i<1480; i++) {
  38.                 for(j=0; j<strlen(file[i]); j++) {
  39.                         if(anc2[j] == anc1[i][j]) {
  40.                                 x++;
  41.                         }
  42.                         if(x == 10) {
  43.                                 printf("String is: %s", file[i]);
  44.                         }
  45.                 }
  46.                 x=0;
  47.         }
  48. }
  49.  
All it does absolutley nothing. Does'nt print out anything, and I don't know why.
The word list is very big.
Aug 23 '07 #1
2 2324
ilikepython
844 Expert 512MB
I've been trying to make a program that takes as input a scramble word,
gets the anscii value of that scrambled word, and numerically sorts it. And tries to find the same numerical order in the word list.

Heres the code:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. //sort the anscci value numerically
  4. void sort(int a[], int len) {
  5.         int j, i, t;
  6.         for(i=len-1; i>=0; i--) {
  7.                 for(j=1; j<=i; j++) {
  8.                         if(a[j-1] > a[j]) {
  9.                                 t = a[j-1];
  10.                                 a[j-1] = a[j];
  11.                                 a[j] = t;
  12.                         }
  13.                 }
  14.         }
  15. }
  16. //get the anscii value of the string.
  17. void getanc(int a[], char b[]) {
  18.         int i;
  19.         for(i=0; i<strlen(b); i++) {
  20.                 a[i] = (int)b[i];
  21.         }
  22. }
  23.  
  24. main(int argc, char *argv[]) {
  25.         char file[1500][11], input[11];
  26.         int anc1[1500][11], anc2[11];
  27.         int i, j, x;
  28.         FILE *word = fopen("wordlist.txt", "r");
  29.         for(i=0; i<1480; i++) {
  30.                 fgets(file[i], 11, word);
  31.                 getanc(anc1[i], file[i]);
  32.                 sort(anc1[i], strlen(file[i]));
  33.         }
  34.         strcpy(input, argv[1]);
  35.         getanc(anc2, input);
  36.         sort(anc2, strlen(input));
  37.         for(i=0; i<1480; i++) {
  38.                 for(j=0; j<strlen(file[i]); j++) {
  39.                         if(anc2[j] == anc1[i][j]) {
  40.                                 x++;
  41.                         }
  42.                         if(x == 10) {
  43.                                 printf("String is: %s", file[i]);
  44.                         }
  45.                 }
  46.                 x=0;
  47.         }
  48. }
  49.  
All it does absolutley nothing. Does'nt print out anything, and I don't know why.
The word list is very big.
You never check if the file is opened correctlly. Try this:
Expand|Select|Wrap|Line Numbers
  1. if (word != NULL)
  2. {
  3.     printf("Could not open the file");
  4.     return 1;
  5. }
  6. else
  7. {
  8.     ... continue ...
  9. }
  10.  
Aug 23 '07 #2
ilikepython
844 Expert 512MB
I've been trying to make a program that takes as input a scramble word,
gets the anscii value of that scrambled word, and numerically sorts it. And tries to find the same numerical order in the word list.

Heres the code:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. //sort the anscci value numerically
  4. void sort(int a[], int len) {
  5.         int j, i, t;
  6.         for(i=len-1; i>=0; i--) {
  7.                 for(j=1; j<=i; j++) {
  8.                         if(a[j-1] > a[j]) {
  9.                                 t = a[j-1];
  10.                                 a[j-1] = a[j];
  11.                                 a[j] = t;
  12.                         }
  13.                 }
  14.         }
  15. }
  16. //get the anscii value of the string.
  17. void getanc(int a[], char b[]) {
  18.         int i;
  19.         for(i=0; i<strlen(b); i++) {
  20.                 a[i] = (int)b[i];
  21.         }
  22. }
  23.  
  24. main(int argc, char *argv[]) {
  25.         char file[1500][11], input[11];
  26.         int anc1[1500][11], anc2[11];
  27.         int i, j, x;
  28.         FILE *word = fopen("wordlist.txt", "r");
  29.         for(i=0; i<1480; i++) {
  30.                 fgets(file[i], 11, word);
  31.                 getanc(anc1[i], file[i]);
  32.                 sort(anc1[i], strlen(file[i]));
  33.         }
  34.         strcpy(input, argv[1]);
  35.         getanc(anc2, input);
  36.         sort(anc2, strlen(input));
  37.         for(i=0; i<1480; i++) {
  38.                 for(j=0; j<strlen(file[i]); j++) {
  39.                         if(anc2[j] == anc1[i][j]) {
  40.                                 x++;
  41.                         }
  42.                         if(x == 10) {
  43.                                 printf("String is: %s", file[i]);
  44.                         }
  45.                 }
  46.                 x=0;
  47.         }
  48. }
  49.  
All it does absolutley nothing. Does'nt print out anything, and I don't know why.
The word list is very big.
Also, move the x = 0 line to the top of the loop because it will be undefined the first time:
Expand|Select|Wrap|Line Numbers
  1.         for(i=0; i<1480; i++) {
  2.                 x = 0;
  3.                 for(j=0; j<strlen(file[i]); j++) {
  4.                         if(anc2[j] == anc1[i][j]) {
  5.                                 x++;
  6.                         }
  7.                         if(x == 10) {
  8.                                 printf("String is: %s", file[i]);
  9.                         }
  10.                 }
  11.             //    x=0;
  12.         }
  13.  
Aug 23 '07 #3

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

Similar topics

9
by: superprad | last post by:
"X-No-Archive: yes" what I am looking for is 1. To create a list of different words of various lengths(1-15) using A-Z,a-z,0-9 and punctuations.Basically anything that could be found on a...
2
by: CV | last post by:
How can I match 'n' number of neighbouring words of a pattern using regular expressions? For example, suppose I am looking for the pattern "length xyz cm" in some text. where xyz is a number -...
8
by: Rick | last post by:
I have a program that reads from a file. In the file are a series of words. I read in all the words into a string array, find the average length, count the number of words, display the longest...
7
by: Sling | last post by:
I code in Rexx on the mainframe which has 2 built-in functions: word(s,i) & words(s). word(s,i) returns the ith word in the s(tring), and words(s) returns the number of words within the s(tring)....
7
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
7
by: abraxas | last post by:
Ok maybe someone here can help me. I have been learning c++ and have been toying with some basic programming and have run into something that I would have assumed would have been fairly easy. I...
7
by: Jay | last post by:
How would I be able to grab random words from an internet source. I'd like to grab a random word from a comprehensive internet dictionary. What would be the best source and the best way to go...
20
by: dmurray14 | last post by:
Hey guys, I'm a C++ newbie here - I've messed with VB, but I mostly stick to web languages, so I find C++ to be very confusing at times. Basically, I am trying to import a text file, but I want...
6
by: Xernoth | last post by:
Hi, I have an exercise that requests the following: Write a function that reads words from an input stream and stores them in a vector. Use that function both to write programs that count the...
14
bugboy
by: bugboy | last post by:
I'm a beginner at this and am confused... I have three tables: 1. words / wordpk, word 2. definitions / definitionspk and definition 3. associations / wordpk, definitionspk 'words' holds...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.