| | |
Scanning an inputted array of words, and separating each word into a new array?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Re: Scanning an inputted array of words, and separating each word into a new array?
0
#2 Nov 29th, 2005
Re: Scanning an inputted array of words, and separating each word into a new array?
0
#3 Nov 30th, 2005
Re: Scanning an inputted array of words, and separating each word into a new array?
0
#4 Nov 30th, 2005
Oh, thanks for the information. I will be sure to look at the string class tonight when I get a chance to finish my program. I assume you are talking about the <string.h> class right? If so, I have tried using the strtok function to break each word into a function. After trying this I cannot seem to figure out how to separate each token into a new array. Or is that even needed to do what I need to do? Do I need to use something other than strtok?
Re: Scanning an inputted array of words, and separating each word into a new array?
0
#5 Nov 30th, 2005
Re: Scanning an inputted array of words, and separating each word into a new array?
0
#6 Nov 30th, 2005
Re: Scanning an inputted array of words, and separating each word into a new array?
0
#7 Nov 30th, 2005
Post your attempt. The basics are something like this:
C Syntax (Toggle Plain Text)
static const char delim[] = " \n"; size_t size = 0; char word[10][20], *ptr = strtok(sentence, delim); while ( ptr != NULL ) { strcpy(word[size], ptr); if ( ++size >= sizeof word / sizeof *word ) { break; } ptr = strtok(NULL, delim); }
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Re: Scanning an inputted array of words, and separating each word into a new array?
0
#8 Nov 30th, 2005
Ok, this is my program. I included the entire thing, because when I went to add the part in the main() function about choosing which game to play, it gives an error when I run it. Something is wrong and I have no clue what it is. My attempt at the arrays is in the cellPhone function which is not complete.
Also my compiler is giving me an error if i try to shorten the line length of the menu by entering half of it to a new line. Why is it doing this?
Also my compiler is giving me an error if i try to shorten the line length of the menu by entering half of it to a new line. Why is it doing this?
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> void nameGame(); void pigLatin(); void changeLatin(char [], int); void cellPhone(); void cellButtons(char [], int); main() { int input = 0; printf("\n\tEnter your choice:\n\t1. Name Game\n\t2. Pig Latin\n\t3. Text Messaging\n\t4. Exit\n"); scanf("%d", &input); while (input != 4) { if (input == 1) nameGame(); else if (input == 2) pigLatin(); else if (input == 3) cellPhone(); else printf("Invalid choice. Enter a number 1-4."); printf("\n\tEnter your choice:\n\t1. Name Game\n\t2. Pig Latin\n\t3. Text Messaging\n\t4. Exit\n"); scanf("%d", &input); } return 0; } void nameGame() { char c, *stringVowel, firstName[25]; const char *stringVowels = "aeiou"; int i = 0; puts("Enter a first name: "); while ( ( c = getchar() ) != '\n') firstName [i++] = c; firstName[i] = '\0'; stringVowel = strpbrk(firstName, stringVowels); if (firstName[0] == 'B' || firstName[0] == 'b') { printf("\n%s!\n", firstName); printf("%s, %s bo %s Bonana fanna fo F%s\nFee fy mo M%s, %s!", firstName, firstName, strstr(firstName, stringVowel), strstr(firstName, stringVowel), strstr(firstName, stringVowel), firstName); } else if (firstName[0] == 'F' || firstName[0] == 'f') { printf("\n%s!\n", firstName); printf("%s, %s bo B%s Bonana fanna fo %s\nFee fy mo M%s, %s!", firstName, firstName, strstr(firstName, stringVowel), strstr(firstName, stringVowel), strstr(firstName, stringVowel), firstName); } else if (firstName[0] == 'M' || firstName[0] == 'm') { printf("\n%s!\n", firstName); printf("%s, %s bo B%s Bonana fanna fo F%s\nFee fy mo %s, %s!", firstName, firstName, strstr(firstName, stringVowel), strstr(firstName, stringVowel), strstr(firstName, stringVowel), firstName); } else if (firstName[0] == 'A' || firstName[0] == 'a') { firstName[0] = 'a'; printf("\n%s!\n", firstName); printf("%s, %s bo B%s Bonana fanna fo F%s\nFee fy mo M%s, %s!", firstName, firstName, firstName, firstName, firstName, firstName); } else if (firstName[0] == 'E' || firstName[0] == 'e') { firstName[0] = 'e'; printf("\n%s!\n", firstName); printf("%s, %s bo B%s Bonana fanna fo F%s\nFee fy mo M%s, %s!", firstName, firstName, firstName, firstName, firstName, firstName); } else if (firstName[0] == 'I' || firstName[0] == 'i') { firstName[0] = 'i'; printf("\n%s!\n", firstName); printf("%s, %s bo B%s Bonana fanna fo F%s\nFee fy mo M%s, %s!", firstName, firstName, firstName, firstName, firstName, firstName); } else if (firstName[0] == 'O' || firstName[0] == 'o') { firstName[0] = 'o'; printf("\n%s!\n", firstName); printf("%s, %s bo B%s Bonana fanna fo F%s\nFee fy mo M%s, %s!", firstName, firstName, firstName, firstName, firstName, firstName); } else if (firstName[0] == 'U' || firstName[0] == 'u') { firstName[0] = 'u'; printf("\n%s!\n", firstName); printf("%s, %s bo B%s Bonana fanna fo F%s\nFee fy mo M%s, %s!", firstName, firstName, firstName, firstName, firstName, firstName); } else { printf("\n%s!\n", firstName); printf("%s, %s bo B%s Bonana fanna fo F%s\nFee fy mo M%s, %s!", firstName, firstName, strstr(firstName, stringVowel), strstr(firstName, stringVowel), strstr(firstName, stringVowel), firstName); } } void pigLatin() { char c, d, sentence[200], tempSentence[200], *wordPtr; int i = 0, j = 0; puts("Enter a sentence: "); while ( ( c = getchar() ) != '\n') sentence [i++] = c; sentence[i] = '\0'; wordPtr = strtok(sentence, " "); i = 0; while (wordPtr != NULL) { printf("%s\t", wordPtr); while ( ( d = getchar() ) != '\0') tempSentence [i++] = d; printf("%s\n", tempSentence); wordPtr = strtok(NULL, " "); } } void changeLatin(char sentence[200], int i) { } void cellPhone() { char c, sentence[200]; int i = 0, j = 0; puts("Enter a sentence: "); while ( ( c = getchar() ) != '\n') sentence [i++] = c; sentence[i] = '\0'; cellButtons(sentence, i); } void cellButtons(char sentence[], int i) { int j = 0; for(j = 0; j < i; j++) { if (sentence[j] == 'A') printf("*2"); else if (sentence[j] == 'B') printf("*22"); else if (sentence[j] == 'C') printf("*222"); else if (sentence[j] == 'D') printf("*3"); else if (sentence[j] == 'E') printf("*33"); else if (sentence[j] == 'F') printf("*333"); else if (sentence[j] == 'G') printf("*4"); else if (sentence[j] == 'H') printf("*44"); else if (sentence[j] == 'I') printf("*444"); else if (sentence[j] == 'J') printf("*5"); else if (sentence[j] == 'K') printf("*55"); else if (sentence[j] == 'L') printf("*555"); else if (sentence[j] == 'M') printf("*6"); else if (sentence[j] == 'N') printf("*66"); else if (sentence[j] == 'O') printf("*666"); else if (sentence[j] == 'P') printf("*7"); else if (sentence[j] == 'Q') printf("*77"); else if (sentence[j] == 'R') printf("*777"); else if (sentence[j] == 'S') printf("*7777"); else if (sentence[j] == 'T') printf("*8"); else if (sentence[j] == 'U') printf("*88"); else if (sentence[j] == 'V') printf("*888"); else if (sentence[j] == 'W') printf("*9"); else if (sentence[j] == 'X') printf("*99"); else if (sentence[j] == 'Y') printf("*999"); else if (sentence[j] == 'Z') printf("*9999"); else if (sentence[j] == 'a') printf("2"); else if (sentence[j] == 'b') printf("22"); else if (sentence[j] == 'c') printf("222"); else if (sentence[j] == 'd') printf("3"); else if (sentence[j] == 'e') printf("33"); else if (sentence[j] == 'f') printf("333"); else if (sentence[j] == 'g') printf("4"); else if (sentence[j] == 'h') printf("44"); else if (sentence[j] == 'i') printf("444"); else if (sentence[j] == 'j') printf("5"); else if (sentence[j] == 'k') printf("55"); else if (sentence[j] == 'l') printf("555"); else if (sentence[j] == 'm') printf("6"); else if (sentence[j] == 'n') printf("66"); else if (sentence[j] == 'o') printf("666"); else if (sentence[j] == 'p') printf("7"); else if (sentence[j] == 'q') printf("77"); else if (sentence[j] == 'r') printf("777"); else if (sentence[j] == 's') printf("7777"); else if (sentence[j] == 't') printf("8"); else if (sentence[j] == 'u') printf("88"); else if (sentence[j] == 'v') printf("888"); else if (sentence[j] == 'w') printf("9"); else if (sentence[j] == 'x') printf("99"); else if (sentence[j] == 'y') printf("999"); else if (sentence[j] == 'z') printf("9999"); else if (sentence[j] == '1') printf("1111111111111111"); else if (sentence[j] == '2') printf("2222"); else if (sentence[j] == '3') printf("3333"); else if (sentence[j] == '4') printf("4444"); else if (sentence[j] == '5') printf("5555"); else if (sentence[j] == '6') printf("6666"); else if (sentence[j] == '7') printf("77777"); else if (sentence[j] == '8') printf("8888"); else if (sentence[j] == '9') printf("99999"); else if (sentence[j] == '0') printf("11111111111111111"); else if (sentence[j] == '.') printf("1"); else if (sentence[j] == '?') printf("11"); else if (sentence[j] == '!') printf("111"); else if (sentence[j] == ',') printf("1111"); else if (sentence[j] == '@') printf("11111"); else if (sentence[j] == '-') printf("1111111"); else printf("#"); } printf("\n"); }
Re: Scanning an inputted array of words, and separating each word into a new array?
0
#9 Nov 30th, 2005
Re: Scanning an inputted array of words, and separating each word into a new array?
0
#10 Nov 30th, 2005
From my earlier mockup:
#include <stdio.h>
#include <string.h>
int main(void)
{
char sentence[200];
puts("enter a sentence:");
fflush(stdout);
if ( fgets(sentence, sizeof sentence, stdin) != NULL )
{
static const char delim[] = " \n";
size_t i, size = 0;
char word[10][20], *ptr = strtok(sentence, delim);
while ( ptr != NULL )
{
strcpy(word[size], ptr);
if ( ++size >= sizeof word / sizeof *word )
{
break;
}
ptr = strtok(NULL, delim);
}
puts("words are:");
for ( i = 0; i < size; ++i )
{
puts(word[i]); /* access to each word */
}
}
return 0;
}
/* my output
enter a sentence:
The quick brown fox jumps over the lazy dog.
words are:
The
quick
brown
fox
jumps
over
the
lazy
dog.
*/ "One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Other Threads in the C Forum
- Previous Thread: project question?
- Next Thread: :sad: plz suggest me some solution
| Thread Tools | Search this Thread |
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string strings structures suggestions systemcall test testautomation unix urboc user voidmain() wab win32api windows.h






