-1

i need to read strings from file in C, and that's work, now i need to find in each string the words that start with a capital letter. Any input?

Example: 1) Windows is a good OS 2) Linux is Open Source

Words with capital letter: Windows, OS, Linux, Open, Source.

#include <stdio.h>

int main()
{
  /* dichiarazioni variabili */

  FILE *fp;
  char vet1[100];
  char vet2[100];

  fp = fopen("file.txt", "r"); /* apro il file contenente la stringa */

  if (fp == NULL) {
    printf("\nIl file non esiste!\n");
  }

  while (!feof(fp)) {
    fgets(vet1, 100, fp);
    printf("%s\n", vet1);
  }

  fclose(fp);
}
dtell
  • 2,488
  • 1
  • 14
  • 29
Max Red
  • 1
  • 1

1 Answers1

1

Detect a Capital letter is as simple as read the "ascii" manual in any GNU / Linux distro where you can find that all capital tetters from A to Z has a hex number from 0x40 to 0x5A respectivily with this in mind:

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


int main( int argc,char *argv[])
{
  char *buffer,c;
  size_t bufsize = 32; // add all the buffer that you need
  size_t characters;
  FILE *fp;
  if(argc != 2)
    return -1;
  buffer = (char *)malloc(bufsize * sizeof(char));
  fp = fopen(argv[1],"r");
  if( fp == NULL)
     return -2;
  while( !feof(fp) )
  {
    characters = getline(&buffer,&bufsize,fp);
    buffer[characters-1]='\0';
    if ( buffer[0] > 0x40 && buffer[0] < 0x5A )
        printf("buffer = %s",buffer);
  }

  return 0;
}
  • What is this: if(argv[1][0] == '\0') ? And Malloc function (?) P.S: Thank's a lot!! PPS: I can't use stdlib.h library. – Max Red Mar 28 '19 at 17:05
  • argv[1][0] is the first char of argv[1] and '\0' is the null termination caracter like simple NULL and if(argv[1][0] == '\0') is to be sure that need a file as input. Glad I could help. Feel free to [accept this answer](https://stackoverflow.com/help/accepted-answer) if you found it useful. – Miguel Ángel Retamozo Sanchez Mar 28 '19 at 17:15
  • 1
    Your argv construct is just bad. If you want to determine the existence of command line arguments use argc. – dtell Mar 28 '19 at 17:18
  • @datell thanks for your recomentation I've just edited . – Miguel Ángel Retamozo Sanchez Mar 28 '19 at 17:21