Quick Ques on String Manipulation
I just want to thank all the people that responded when I first posted a question about a project I'm working on... Esp. Elysia. Very Helpful.
I had two quick questions:
The following program is going to be used to format text to certain standards set by our senior editors for a new organization that I work for. In the latter parts of the program, I'm trying to recognize the strings for the date, and I clearly want to be able to use a for loop, but obv you can't increment a string value. Is there a way to implement atoi() or something similar to be able to recognize that part of the string, then convert it to be able to make it more efficient? You'll clearly see the inefficiency that is curr. in place in the lower parts of the date_func().
And is there any way to implement regular expressions in C? Sorry if this is a stupid question, I"m def. still a noobie, but really trying to push myself forward with a workable and useful project.
Thanks in advance for your responses.
This board is a great resource.
-Chris
===============================
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int url_func();
int title_func();
int author_func();
int source_func();
int date_func();
FILE *stream_f1;
FILE *stream_f2;
char *my_string;
int i = 0;
long position = 0;
int main() {
int c;
int buffersize = 100;
int charsRead = 0;
my_string = (char *) malloc (buffersize * sizeof(char));
stream_f1 = fopen ("sub.txt", "r");
stream_f2 = fopen ("formatted_sub.txt", "w");
do {
charsRead = getline (&my_string, &buffersize, stream_f1);
if (charsRead == -1) break;
while (i <= 4) {
if (i == 0) {
position = ftell (stream_f1);
url_func();
}
if (i == 1) {
date_func();
}
/*
if (i == 1) {
title_func();
}
if (i == 2) {
author_func();
}
if (i == 3) {
source_func();
}
*/
break;
}
if (i > 4) {
fprintf (stream_f2, "%s", my_string);
printf("%s", my_string);
}
else fseek (stream_f1, position, SEEK_SET);
} while (charsRead >= 0);
printf ("\n");
fprintf (stream_f2, "\n");
free(my_string);
fclose (stream_f1);
fclose (stream_f2);
return 0;
}
/* Function to obtain URL string *******************************************************
*********************************************************************************************/
int url_func() {
if (strstr (my_string, "http")) {
++i;
printf ("%s", my_string);
fprintf (stream_f2, "s", my_string);
}
return 0;
}
/* Function to obtain date string *******************************************************
**********************************************************************************************/
int date_func() {
char *day_num;
char *aa, *bb, *cc, *dd;
do {
if (strstr (my_string, "mon") || strstr (my_string, "Mon") || strstr (my_string, "MON")) {
aa = "Monday";
}
else if (strstr (my_string, "tues") || strstr (my_string, "Tues") || strstr (my_string, "TUES")) {
aa = "Tuesday";
}
else if (strstr (my_string, "wed") || strstr (my_string, "Wed") || strstr (my_string, "WED")) {
aa = "Wednesday";
}
else if (strstr (my_string, "thurs") || strstr (my_string, "Thurs") || strstr (my_string, "THURS")) {
aa = "Thursday";
}
else if (strstr (my_string, "fri") || strstr (my_string, "Fri") || strstr (my_string, "FRI")) {
aa = "Friday";
}
else if (strstr (my_string, "sat") || strstr (my_string, "Sat") || strstr (my_string, "SAT")) {
aa = "Saturday";
}
else if (strstr (my_string, "sun") || strstr (my_string, "Sun") || strstr (my_string, "SUN")) {
aa = "Sunday";
}
else ;
}
// Next Do Statement
do {
if (strstr (my_string, "01") || strstr (mystring, "1")) {
bb = "01";
}
if (strstr (my_string, "02") || strstr (mystring, "2")) {
bb = "02";
}
if (strstr (my_string, "03") || strstr (mystring, "3")) {
bb = "03";
}
if (strstr (my_string, "04") || strstr (mystring, "4")) {
bb = "04";
}
if (strstr (my_string, "05") || strstr (mystring, "5")) {
bb = "05";
}
if (strstr (my_string, "06") || strstr (mystring, "6")) {
bb = "06";
}
if (strstr (my_string, "07") || strstr (mystring, "7")) {
bb = "07";
}
if (strstr (my_string, "08") || strstr (mystring, "8")) {
bb = "08";
}
if (strstr (my_string, "09") || strstr (mystring, "9")) {
bb = "09";
}
if (strstr (my_string, "10") {
bb = "10";
}
if (strstr (my_string, "11") {
bb = "11";
}
if (strstr (my_string, "01") || strstr (mystring, "1")) {
bb = "01";