'How to read integers from file with integers and strings in C

I am reading a .txt file which contains data in a random form i.e. it contains integers and strings mixed in it.

Sample .txt file:

this is a 22 string 33 sorry222 stack33ing
still yysi288 2nd line

I want to read the file and differentiate all valid string i.e. which do not contain integers concatinated with them. And store those strings in an array.

Any leads on how to differentiate?



Solution 1:[1]

First: I can´t write the programm for you. It is your task to do and beside this i can´t even change things on your code or at least suggest to alter, because you are not providing any code. I can give you only a dusty leading algorithm for this case:

All the words you might see, are not valid strings in the file. It is only one string containing white space characters between each sequence of characters which appear for you as one word or separated string, but it doesn´t.

You have to get the whole string from the file first and store it into an char array, lets name it source, using fgets():

#include <stdio.h>

FILE *input;
char source[200];

input = fopen("text.txt","r");

fgets(source, 200, input);

After that you need to make those "words" separated strings by transport each of the characters of the source string, one after another, to other char arrays and quit writing the characters to them as soon as the space character or the NUL-byte after the last word is provided in the source string. Don´t forget to make a Nul-Byte (\n) terminating each string.

Thereafter you check each, now valid and separated string, if it either is a string that contains numbers or a string without any number.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1