Integer array with whitespace characters output - c

I am trying to write a c program. It have to enter two arrays and input should be space seperated. I tried somehow to eliminate the '\n'.
#include <stdio.h>
int main()
{
char temp;
int alc[3]={0}, bob[3]={0}, i=0;
//enter alice
do
{
scanf("%d%c", &alc[i], &temp);
i++;
} while(temp != '\n');
i=0;
//enter bob
do
{
scanf("%d%c", &bob[i], &temp);
i++;
} while(temp != '\n');
//print alice
for(i = 0; i < 3 ; i++)
{
printf("%d ", alc[i]);
}
//print bob
for(i = 0; i < 3 ; i++)
{
printf("%d ", bob[i]);
}
return 0;
}
output ./a.out
5 6 7
3 6 10
5 6 7 3 6 10
Is there a better way to do same?

The idea is get the line as input and then parse it to get the integers using strtol etc. The line you will get using fgets. And then you will store it in array. There are two options now,
If you get more elements than you can hold in the array then you will show error when the array is full.
Or use dynamically allocated memory which will grow as the number you enter increase.
I am afraid, using scanf until you get integers is an option - but that is not the good idea and scanf is not the easy way to go about this.

the following proposed code:
cleanly compiles
follows the axiom: only one statement per line and (at most) one variable declaration per statement.
is consistently indented
eliminates unneeded variables
limits the scope of variables
performs the desired functionality
properly checks for error indications from system functions
documents why each header file was included
all 'magic' numbers given meaningful names (via #define statement)
and now the proposed code:
#include <stdio.h> // scanf(), fprintf(), stderr, printf()
#include <stdlib.h> // exit(), EXIT_FAILURE
#define MAX_NUMS_PER_PERSON 3
int main( void )
{
int alice[ MAX_NUMS_PER_PERSON ]={0};
int bob[ MAX_NUMS_PER_PERSON ]={0};
//enter alice
for( int i=0; i< MAX_NUMS_PER_PERSON; i++ )
{
if( 1 != scanf("%d", &alice[i]) )
{
fprintf( stderr, "failed to input nums for Alice\n" );
exit( EXIT_FAILURE );
}
// implied else, scanf successful
}
//enter bob
for( int i=0; i< MAX_NUMS_PER_PERSON; i++ )
{
if( 1 != scanf("%d", &bob[i]) )
{
fprintf( stderr, "failed to input nums for Bob\n" );
exit( EXIT_FAILURE );
}
// implied else, scanf successful
}
//print alice
for( int i = 0; i < MAX_NUMS_PER_PERSON; i++)
{
printf("%d ", alice[i]);
}
//print bob
for( int i = 0; i < MAX_NUMS_PER_PERSON; i++)
{
printf("%d ", bob[i]);
}
return 0;
}

Changed my C program according to this answer
Putting numbers separated by a space into an array
#include <stdio.h>
#include <ctype.h>
#define ARRAY_SIZE 3
#define BOB_SIZE 5
#define ALICE_SIZE 4
int main()
{
int tmp, i=0;
char follow;
int count;
int a[ALICE_SIZE]={0}, b[BOB_SIZE]={0};
if((ALICE_SIZE < ARRAY_SIZE) || (BOB_SIZE < ARRAY_SIZE))
{
printf("Not sufficient space in array, check the sizes.\n");
return -1;
}
while ((i < ARRAY_SIZE) && (count = scanf("%d%c", &tmp, &follow)) > 0)
{
if ((count == 2 && isspace(follow)) || (count == 1))
{
a[i++] = tmp;
}
else
{
printf ("Bad character detected: %c\n", follow);
break;
}
}
i=0;
while ((i < ARRAY_SIZE) && (count = scanf("%d%c", &tmp, &follow)) > 0)
{
if ((count == 2 && isspace(follow)) || (count == 1))
{
b[i++] = tmp;
}
else
{
printf ("Bad character detected: %c\n", follow);
break;
}
}
for(i = 0; i < ARRAY_SIZE ; i++)
printf("%d ", a[i]);
printf("\n");
for(i = 0; i < ARRAY_SIZE ; i++)
printf("%d ", b[i]);
return 0;
}
Tried to make input as general as possible while using scanf
looks like too much effort but its required to make it robust,and put corner cases and errors.
More problems with scanf comes with strings or %s specifier. So better get used to fgets, strtol and dynamic arrays in parsing while giving inputs.

Related

How to get sequence of numbers and then print the last 5?

Im trying to make a program that will get sequence from the user that end with 0, and then i want to print the last 5 numbers (not including the 0).
I can assume that the user will input all the numbers in one line and will end it with 0.
I wrote that code but something is wrong with it, I think its something about the scanf line.
Input:
1 6 9 5 2 1 4 3 0
Output: no output
#include <stdio.h>
#define N 5
int main()
{
int arr[N] = {0};
int last_input, j;
printf("please enter more than %d number and than enter 0: \n", N);
last_input = 0;
while (last_input<N) {
scanf(" %d", &j);
if (j == '0') {
last_input = N;
break;
}
else {
arr[last_input] = j;
}
if (last_input==(N-1)) {
last_input=-1;
}
++last_input;
}
printf("The last %d numbers u entered are:\n", N);
for (j=(last_input+1); j<N; ++j) {
printf(" %d", arr[j]);
}
for (j=0; j<last_input; ++j) {
printf(" %d", arr[j]);
}
return 0;
}
This comparison
if (j == '0') {
does not make a sense because the user will try to enter the integer value 0 instead of the value (for example ASCII 30h or EBCDIC F0h) for the character '0'.
You need to write at least
if (j == 0) {
Due to these sub-statements of the if statement
last_input = N;
break;
this for loop
for (j=(last_input+1); j<N; ++j) {
printf(" %d", arr[j]);
}
is never executed and does not make a sense.
This statement
last_input=-1;
results in breaking the order of the N last elements in its output. And moreover the result value of the variable last_input will be incorrect.
You need to move elements of the array one position left. For this purpose you can use a loop of standard C function memmove.
The program can look the following way.
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { N = 5 };
int arr[N];
printf( "Please enter at least not less than %d numbers (0 - stop): ", N );
size_t count = 0;
for (int num; scanf( "%d", &num ) == 1 && num != 0; )
{
if (count != N)
{
arr[count++] = num;
}
else
{
memmove( arr, arr + 1, ( N - 1 ) * sizeof( int ) );
arr[N - 1] = num;
}
}
if (count != 0)
{
printf( "The last %zu numbers u entered are: ", count );
for (size_t i = 0; i < count; i++)
{
printf( "%d ", arr[i] );
}
putchar( '\n' );
}
else
{
puts( "There are no entered numbers." );
}
}
The program output might look like
Please enter at least not less than 5 numbers (0 - stop): 1 2 3 4 5 6 7 8 9 0
The last 5 numbers u entered are: 5 6 7 8 9
I made some changes based on ur comments and now its work fine!
#include <stdio.h>
#define N 5
int main()
{
int arr[N] = {0};
int last_input, j;
printf("please enter more than %d number and than enter 0: \n", N);
last_input = 0;
while (last_input<N) {
scanf("%d", &j);
if (j == 0) {
break;
}
else {
arr[last_input] = j;
}
if (last_input==(N-1)) {
last_input=-1;
}
++last_input;
}
printf("The last %d numbers u entered are:\n", N);
for (j=(last_input); j<N; ++j) {
printf("%d ", arr[j]);
}
for (j=0; j<last_input; ++j) {
printf("%d ", arr[j]);
}
return 0;
}
thank u guys <3.

Optimize code to get following output in c

I had an interview last week. They asked me to write a code to print like this
input :5
0
101
21012
3210123
432101234
54321012345
i wrote the below code but he said i could optimize this more . i cant figure it out.
,
int main(){
int n,i,j,k,lim,num;
scanf("%d",&n);//getting input starting number of last row
lim=n;
int collen=n+2;//it denotes end of row
for(i=0;i<n+1;i++)
{
num=i;
k=0;
for(j=0;j<collen-1;j++){
if(j<lim)
printf(" ");
else if(num<0){
printf("%d",++k);
}
else{
printf("%d",num--);
}
}//j for
printf("\n");
collen++;
lim--;
}//i for
}// main end
I have different code at first attempt, I used flag to detect when num reaches for incrementing and decrementing, it was complex there was about 4 if inside second loop, so I optimized that code to the above one. He said can you optimize more? I have no idea to optimize it .
My question: can it be optimized? If it can be - please post the code
There are more for-loops than those comparing by <. for(i=0;i<n+1;i++) is much clearer written as for (i = 0; i <= n; i++).
If you initialize a value, in example int collen=n+2;, and later use it like collen-1, save the subtraction and initialize it adjusted.
Separate this complex inner loop with ifs into their own loops.
Use less variables.
Use more and consistent whitespace.
And now my solution, but as yours it can only handle inputs from 0 to 9:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int n;
printf("input :");
if (scanf("%d", &n) != 1 || n < 0 || n > 9) {
printf("input not recognized or invalid\n");
return EXIT_FAILURE;
}
for (int i = 0; i <= n; ++i) {
printf("%*d", n - i + 1, i);
for (int j = i - 1; j >= 0; --j) {
printf("%d", j);
}
for (int j = 1; j <= i; ++j) {
printf("%d", j);
}
printf("\n");
}
return EXIT_SUCCESS;
}
This looks optimized to the brims:
int main( int argc, char **argv )
{
puts( "input :5");
puts( " 0");
puts( " 101");
puts( " 21012");
puts( " 3210123");
puts( " 432101234");
puts( "54321012345");
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* Print a pyramid */
int
main(int argc, char **argv)
{
int size = argc > 1 ? strtol(argv[1],NULL,10) : 5;
if( size > 9 || size < 0) {
fprintf(stderr, "Invalid size\n");
return EXIT_FAILURE;
}
for(int line = 0; line <= size; line++) {
char template[]="9876543210123456789";
char *s = template + 9 - size;
template[10 + line] = '\0';
memset(s, ' ', size - line);
if(puts(s) == EOF) {
perror("puts");
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}

Segmentation fault in c programming...pass or fail program where in last the list of pass and fail student

This program have some segmentation fault which is not solved by me please anyone can solve the program.
I also include the string header file then also error comes..
What's the problem
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
int main() {
int num[5], i, j, k;
char name[50];
for (i = 0; i < 5; i++)
{
printf("\nENTER THE NAME:");
scanf("\n %s", &name[i]);
printf("\nENTER THE PERCENTAGE:");
scanf("\n %d", &num[i]);
}
printf("\nTHE STUDENT PASS IN EXAM IS:");
for (j = 0; j < 5; j++)
{
if (num[j] >= 70)
{
printf("\n%s", name[j]);
}
}
{
printf("\nFAIL");
for (k = 0; k < 5; k++)
{
if (num[k] < 70)
{
printf("\n%s", name[k]);
}
}
}
}
from your code i see that you are assigning name to single variable
char nam[40]
if you give input it will store like
'e.g
input="name"
a[0]=n,a[1]=a,a[2]=m
so you cant ginve input more than 1 name and also it will replace 1st name with new one
my solution is to this will be using two dimensional variable
updated code:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
int main() {
int num[5], i, j, k;
char name[5][50];
for (i = 0; i < 5; i++)
{
printf("\nENTER THE NAME:");
scanf("\n %s", name[i]);
printf("\nENTER THE PERCENTAGE:");
scanf("\n %d", &num[i]);
}
printf("\nTHE STUDENT PASS IN EXAM IS:");
for (j = 0; j < 5; j++)
{
if (num[j] >= 70)
{
printf("\n%s", name[j]);
}
}
printf("\nFAIL");
for (k = 0; k < 5; k++)
{
if (num[k] < 70)
{
printf("\n%s", name[k]);
}
}
The following proposed code:
does not include header files that are not used 'conio.h' and 'string.h'
properly checks for I/O errors
uses meaningful names rather than 'magic' numbers for 5, 50. 70
removes an unneeded set of braces '{' and '}'
Makes use of a 2 dimensional array for the student names
limits the scope of variables: 'i' 'k' 'j'
uses a proper signature for function: main()
follows the axiom: only one statement per line and (at most) one variable declaration per statement.
separates code blocks (in this case the 'for()' code blocks) via a single blank line
for ease of readability, includes a space inside parens, after semicolons, around C operators, etc
uses a meaningful name 'grade' rather than the meaninless 'num'
properly outputs error messages to stderr
and now, the proposed code:
#include <stdio.h>
#include <stdlib.h>
#define MIN_PASSING_GRADE 70
#define MAX_STUDENTS 5
#define MAX_NAME_LEN 50
int main( void )
{
int grade[ MAX_STUDENTS ];
char name[ MAX_STUDENTS ][ MAX_NAME_LEN ];
for ( int i = 0; i < MAX_STUDENTS; i++ )
{
printf( "\nENTER THE NAME:" );
if( scanf( " %49s", name[i] ) != 1 )
{
fprintf( stderr, "failed to input student name\n" );
exit( EXIT_FAILURE );
}
printf( "\nENTER THE PERCENTAGE:" );
if( scanf( " %d", &grade[i] ) != 1 )
{
fprintf( stderr, "failed to input student grade\n" );
exit( EXIT_FAILURE );
}
}
printf( "\nTHE STUDENT PASS IN EXAM IS:" );
for ( int j = 0; j < MAX_STUDENTS; j++ )
{
if ( grade[j] >= MIN_PASSING_GRADE )
{
printf( "\n%s", name[j] );
}
}
printf( "\nFAIL" );
for ( int k = 0; k < MAX_STUDENTS; k++ )
{
if ( grade[k] < MIN_PASSING_GRADE )
{
printf( "\n%s", name[k] );
}
}
}

C program for assigning elements of a matrix without letters

I'm having trouble outputting an invalid statement if the user inputs a letter instead of a number into a 2D array.
I tried using the isalpha function to check if the input is a number or a letter, but it gives me a segmentation fault. Not sure what's wrong any tips?
the following code is just the part that assigns the elements of the matrix.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 10
void display(int matrix[][MAX], int size);
int main() {
int n, degree;
int matrix[MAX][MAX];
printf("Enter the size of the matrix: "); // assigning size of the matrix
scanf("%d", &n);
if (n <= 1 || n >= 11) { // can't be bigger than a 10x10 matrix
printf("Invalid input.");
return 0;
}
for (int i = 0; i < n; ++i) { // assigning the elements of matrix
printf("Enter the row %d of the matrix: ", i);
for (int j = 0; j < n; ++j) {
scanf("%d", &matrix[i][j]);
if (!isalpha(matrix[i][j])) { // portion I'm having trouble with
continue;
} else {
printf("Invalid input.");
return 0;
}
}
}
...
As the value of n will be number, we can solve it using string instead of int.
char num[10];
int n;
scanf("%s", num);
if(num[0] < '0' || num[0] > '9' || strlen(num) > 2){
printf("invalid\n");
}
if(strlen(num) == 1) n = num[0] - '0';
if(strlen(num) == 2 && num[0] != 1 && num[1] != 0) printf("invalid\n");
else n = 10;
Also we can use strtol() function to convert the input string to number and then check for validity.You can check the following code for it. I have skipped the string input part. Also you have to add #include<stdlib.h> at the start for the strtol() function to work.
char *check;
long val = strtol (num, &check, 10);
if ((next == num) || (*check != '\0')) {
printf ("invalid\n");
}
if(val > 10 || val < 0) printf("invalid\n");
n = (int)val; //typecasting as strtol return long
You must check the return value of scanf(): It will tell you if the input was correctly converted according to the format string. scanf() returns the number of successful conversions, which should be 1 in your case. If the user types a letter, scanf() will return 0 and the target value will be left uninitialized. Detecting this situation and either aborting or restarting input is the callers responsibility.
Here is a modified version of your code that illustrates both possibilities:
#include <stdio.h>
#define MAX 10
void display(int matrix[][MAX], int size);
int main(void) {
int n, degree;
int matrix[MAX][MAX];
printf("Enter the size of the matrix: "); // assigning size of the matrix
if (scanf("%d", &n) != 1 || n < 2 || n > 10) {
// can't be bigger than a 10x10 matrix nor smaller than 2x2
// aborting on invalid input
printf("Invalid input.");
return 1;
}
for (int i = 0; i < n; i++) { // assigning the elements of matrix
printf("Enter the row %d of the matrix: ", i);
for (int j = 0; j < n; j++) {
if (scanf("%d", &matrix[i][j]) != 1) {
// restarting on invalid input
int c;
while ((c = getchar()) != '\n') {
if (c == EOF) {
printf("unexpected end of file\n");
return 1;
}
}
printf("invalid input, try again.\n");
j--;
}
}
}
...
The isdigit() library function of stdlib in c can be used to check if the condition can be checked.
Try this:
if (isalpha (matrix[i][j])) {
printf ("Invalid input.");
return 0;
}
So if anyone in the future wants to know what I did. here is the code I used to fix the if statement. I am not expecting to put any elements greater than 10000 so if a letter or punctuation is inputted the number generated will be larger than this number. Hence the if (matrix[i][j] > 10000). May not be the fanciest way to do this, but it works and it's simple.
for (int i = 0; i < n; ++i) { // assigning the elements of matrix
printf("Enter the row %d of the matrix: ", i);
for (int j = 0; j < n; ++j) {
scanf("%d", &matrix[i][j]);
if (matrix[i][j] > 10000) { // portion "fixed"
printf("Invlaid input");
return 0;
}
}
}
I used a print statement to check the outputs of several letter and character inputs. The lowest out put is around and above 30000. So 10000 I think is a safe condition.

String Sort and remove Duplicates

First I apologize for any mistype, for I am Brazilian and English is not my native language.
I am a freshman at my college and I got this algorithm to solve, from my teacher:
Make a program that creates a vector of n words, n being a size entered by the user (maximum 100). Your program should remove all duplicate words from the input vector and sort the words. Print the final vector without repeated and ordered words.
E.g. with 7 words to sort:
Input: 7 [enter]
hand ear leg hand hand leg foot
Output: ear foot hand leg
Note: Comment the program prints so that the output of the program is as shown in the example above (the numbers are separated by a spacebar, without space after last digit).
Note2: In case of invalid entry the program should print: "invalid entry" (all lower case).
Ok, I got it working but the I got confused with the notes and I can't find a way to fix the possible bugs, here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char word[100][100], aux[100];
int i, j, num;
printf("Type how many words you want to order: ");
do
{
scanf("%d", &num);
}while (num>100 || num<=0);
for(i=0; i<num; i++)
scanf("%s",&word[i]);
for (i = 0; i < num; i++) //loop to sort alphabetically
{
for (j = i+1; j < num; j++)
{
if ((strcasecmp(word[i], word[j]) > 0)) //swapping words
{
strcpy(aux, word[j]);
strcpy(word[j], word[i]);
strcpy(word[i], aux);
}
}
}
for (i = 0; i < num; i++) //loop to remove duplicates
{
if ((strcasecmp(word[i], word[i+1]) == 0)) //finding the duplicates
{
for (j = i+1; j < num; j++) //loop to delete it
strcpy(word[j], word[j+1]);
num--;
i--;
}
}
printf("\nWords sorted and without duplicates:\n");
for(i=0; i<num-1; i++)
printf("%s ", word[i]); //output with spacebar
printf("%s", word[num-1]); //last output without spacebar
return 0;
}
When I type a word with more than 100 characters, the Code::Blocks closes with an error, else it works fine. What do you think I should change?
The teacher uses a Online Judge (Sharif Judge) to evaluate if the code is right, and I got error in 3 of the tests (that are not specified), all of them were "Time Limit Exceeded". Maybe it has do to with the size of the matrix, or the problem with words >100.
Thanks in advance, Vinicius.
I guess you input sanity check is causing the issue.
As mentioned in the comment section.
If n is always < 100. Definitely your sorting is not causing any time limit exceeded.
Looks like the n is given something greater than 100 and your scanf is waiting and causing the issue. Also, make sure your input numbers are taken properly. If the input is > 100 print 'invalid entry'.
Something like below should work.
scanf("%d", &num);
if (num > 100)
printf("invalid entry");
for (i = 0; i < num; i++) {
scanf("%s", word[i]);
if (strlen(word[i])>100)
printf("invalid entry");
}
Hope it helps!
of course you will get an error if you use woerds more than 100 length casue you
have this line: char word[100][50], aux[100];
that means that you word length limit is set to 50. use word[100][100];
also you may not delete duplicates, just skip them in output
lol of course if youre using judge , you should not output any symbols except the answer, this means you should delete all lines, like :
printf("Type how many words you want to order: ");
and check the input format, and check limitations, i mean max word length , max amounts of words
try smth like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define max_word_length = 101;
#define max_amount_of_words = 101;
int main() {
char word[max_amount_of_words][max_word_length] = {};
char aux[max_word_length];
int i, j, num;
scanf("%d", &num);
if (num < 0 || num > 100) {
printf("invalid entry");
return 0;
}
for (i = 0; i < num; i++) {
scanf("%s", word[i]);
}
for (i = 0; i < num; i++) {//loop to sort alphabetically
for (j = i + 1; j < num; j++) {
if ((strcasecmp(word[i], word[j]) > 0)) { //swapping words
strcpy(aux, word[j]);
strcpy(word[j], word[i]);
strcpy(word[i], aux);
}
}
}
bool is_joint = false;
for (i = 0; i < num; i++) { //loop to skip duplicates
if ((strcasecmp(word[i], word[i + 1]) != 0)) { //if there is a duplicate , we willnot output it
if(is_joint) printf(" ");
printf("%s ", word[i]);
is_joint = true;
}
}
return 0;
}
I got 100% on Judge, I fixed the code and looks like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char word[101][101],aux[101]; //a number higher than the limit to comparisons
int i,j,num;
scanf("%d",&num);
if(num<=0||num>100){ // if words < 0 or >100
printf("invalid input");
return 0;
}
for(i=0;i<num;i++){
scanf("%s",&word[i]); //read n words
if(strlen(word[i])>100){ //if word >100 caracters
printf("invalid input");
return 0;
}
for(j=0;j<strlen(word[i]);j++){
if (word[i][j]>=65&&word[i][j]<=90){
word[i][j]= word[i][j]+32; // if word is uppercase, make them lowcase
}
else if (word[i][j]>122||word[i][j]<97){// if word is different from alphabet lowercase
printf("invalid input");
return 0;
}
}
}
for(i=0;i<num;i++){
for(j=i+1;j<num;j++){
if((strcmp(word[i],word[j])>0)){ //loop to sort words
strcpy(aux,word[j]);
strcpy(word[j],word[i]);
strcpy(word[i],aux);
}
}
}
for(i=0;i<num-1;i++){
if((strcmp(word[i],word[i+1])!=0)){ // output words with spacebar, without the last one
printf("%s ",word[i]);
}
}
printf("%s",word[num-1]); // last word without spacebar
return 0;
}
Thank you everyone who tried to help, I've learned a lot with your suggestions!
This project is actually pretty tough assignment for a programmer who just
started in C.
Run this program in your computer.
Before running against the Judge, make sure you run many times with your manual inputs. Once you are happy with the tests, try against the Judge.
Like I said, the hardest part is storing the user's inputs according to spec (accepting space or newline characters in multiple lines).
#include <stdio.h>
#include <string.h>
int
main(void)
{
int iNumW, iIndex;
int iWCnt = 0;
int iC;
char caTemp[100];
char caWords[100][100];
char *cpDelimeter = " \n";
char *cpToken;
char *cp;
short sIsWord = 1;
char caGarbage[100];
scanf("%d", &iNumW );
fgets(caGarbage, sizeof caGarbage, stdin); //Remove newline char
//Get word inputs
while( iWCnt < iNumW )
{
fgets(caTemp, sizeof caTemp, stdin );
for( cpToken = strtok( caTemp, cpDelimeter ); cpToken != NULL; cpToken = strtok( NULL, cpDelimeter)){
cp = cpToken;
while( *cp ){
sIsWord = 1;
//Check if alphabet
if( !isalpha(*cp) ){
sIsWord = 0;
break;
}
cp++;
}
if( sIsWord ){
strcpy( caWords[iWCnt], cpToken );
//printf( "%s\n", caWords[iWCnt]);
iWCnt++;
if( iWCnt >= iNumW ) break;
} else {
printf("invalid entry.\n");
}
//printf("%d\n", iWCnt);
}
}
int i,j ;
for (i = 0; i < iWCnt; i++) {//loop to sort alphabetically
for (j = i + 1; j < iWCnt; j++) {
if ((strcasecmp(caWords[i], caWords[j]) > 0)) { //swapping words
strcpy(caTemp, caWords[j]);
strcpy(caWords[j], caWords[i]);
strcpy(caWords[i], caTemp);
}
}
}
for (i = 0; i < iWCnt; i++) { //loop to skip duplicates
if ((strcasecmp(caWords[i], caWords[i + 1]) != 0)) { //if there is a duplicate , we willnot output it
printf("%s ", caWords[i]);
}
}
return 0;
}

Resources