0

I have a problem with an homework. I need to read strings from file and i need to find words that begin with a Capital Letter, and i need to write these in a new vector. The step 1 is done, i need to use function for these steps, and i can't use library except <stdio.h>

Example:

Original String: the Pen is Red New Vector: Pen, Red

My code:

#include <stdio.h>
#define dim 150
void lettura(int vett1[]); // reading function
void capitalLetter(int vett1[], int vett2[], int i);

main()
{    
    char vett1[dim];
    char vett2[dim];
    int i;
    lettura(vett1);
    capitalLetter(vett1, vett2, i);
}

void lettura(int vett1[])
{
    FILE *fp;
    char nomefile[dim];
    printf("--> Inserisci il nome del file: \n\n", &nomefile); // "insert the name of the file"
    gets(nomefile);
    printf("\n--> Il contenuto del file e' il seguente: \n\n"); // "the content of the file is the following"
    fp= fopen("file.txt","r");
    while(!feof(fp)) {
    fgets(vett1, dim, fp);
    printf("%s", vett1);

    }  // I close the while

    fclose(fp);
}  // I close the function


void capitalLetter(int vett1[], int vett2[], int i)
{
    for(i=0; i<dim; i++){
    if((vett1[i]>= 'A') && (vett1[i]<= 'Z'))
    vett1=vett2;
    }

    printf("%s", vett2);
}
mrflash818
  • 930
  • 13
  • 24
  • 1
    Please try to edit the question and fix the formatting. It needs consistent indention and less empty lines. – Lundin Mar 29 '19 at 13:40
  • 2
    [Never use `gets`](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used)!!! – Fred Larson Mar 29 '19 at 13:41
  • 1
    main is supposed to return a value, an int, you know. – machine_1 Mar 29 '19 at 13:44
  • Please [edit] your question to show how the contents of your input file looks like. Does the file contain normal text with several words per line or only one word per line? `void lettura(int vett1[])` does not match the type of `char vett1[dim];` which you are passing to the function. Do you want to get a list of words? When `lettura` returns, `vett1` will contain the last line you read from the file. Both `vett1` and `vett2` are arrays of characters, not arrays of strings (lines from the file). Probably you should process every line with `capitalLetter` after reading it in `lettura` – Bodo Mar 29 '19 at 13:54
  • Ok, i updated the post. I just added an example – beatrice Mar 29 '19 at 14:09
  • Just as a suggestion, use the [isupper](http://pubs.opengroup.org/onlinepubs/009696899/functions/isupper.html) function instead of testing against `'A'` and `'Z'` - it makes things a little simpler, and can deal with different locales more easily. – John Bode Mar 29 '19 at 14:39

1 Answers1

0

The first problem with your capitalLetter function is the parameters are defined incorrectly.

void capitalLetter(int vett1[], int vett2[], int i);

This is expecting you to pass in 2 arrays of int, but you're passing it in two arrays of char. Your compiler should be throwing up some warnings at this difference of types I'd hope. You'd want it to look like this:

void capitalLetter(char vett1[], char vett2[], int i);

Your loop is likely to go beyond the end of the string as you don't check for the end of it - you check for the size of the whole array. You should stop the loop when you find a NUL character like this.

for(i=0; vett1[i]!='\0'; i++)

I don't know what you were trying to do with this line:

vett1=vett2;

but it doesn't make any sense since it would be replacing the string you're processing with an empty one. If you want vett2 to contain the capital letters you want to change that line to

vett2[j++]=vett1[i];

which as you can probably guess also means added a new variable j. It populates the array vett2 with each capital letter and uses j so that it starts at the beginning and fills it up as it goes along since you can't use i as that will leave gaps.

You are also passing in the variable i for no readily apparent reason too. You can declare a local copy in the function itself, along with j.

int i, j = 0;

And once outside of the loop you need to add a NUL terminator to vett2 to make it an actual string.

vett2[j]='\0';

The end result would look something like this:

void capitalLetter(char vett1[], char vett2[]) {
    int i, j = 0;
    for(i=0; vett1[i]!='\0'; i++) {
        if((vett1[i]>= 'A') && (vett1[i]<= 'Z')) {
            vett2[j++]=vett1[i];
        }
    }
    vett2[j]='\0';
    printf("%s", vett2);
}
Chris Turner
  • 8,082
  • 1
  • 14
  • 18
  • Ah, never mind - I've just spotted a really stupid mistake – Chris Turner Mar 29 '19 at 14:13
  • stupid mistake on my part, I hasten to add - I should have spotted you'd defined the function parameters incorrectly – Chris Turner Mar 29 '19 at 14:19
  • Ok, I understand the mistakes I made, but the program still doesn't work. It does not show me the new vector with the letters having the capital letter – beatrice Mar 29 '19 at 14:35
  • Are you sure that `vett1` contains capital letters? If you comment out the call to `lettura` and add `strcpy(vett1,"Hello World");`, you should get "HW" as the output – Chris Turner Mar 29 '19 at 14:38