Notes![what is notes.io? What is notes.io?](/theme/images/whatisnotesio.png)
![]() ![]() Notes - notes.io |
#include <string.h>
FILE *openFile(char fileName[]){
FILE *file = NULL;
if((file = fopen(fileName, "r"))==NULL){
printf("CANNOT open %sn", fileName);
}
return file;
}
void writeFile(char fileName[], char ssData[], char oStringData[][50], int nrIOfileLines) {
FILE *file = NULL;
if((file = fopen(fileName, "a"))==NULL){
printf("CANNOT open %sn", fileName);
}
int i, j;
// write substrs
for (i=0;i<strlen(ssData);i++){
fprintf(file, "%c", ssData[i]);
}
fprintf(file, "n");
// write output for substrs
for (i=0; i<nrIOfileLines; i++) {
for (j=0; j<strlen(oStringData[i]); j++) {
fprintf(file, "%c",oStringData[i][j]);
}
}
fprintf(file, "%c", 'n');
fclose(file);
return 0;
}
int isPalindrome(char str[]){
int len=strlen(str);
int j=len-1;
int i;
for (i=0;i<(int)len/2;i++){
if (str[i]!=str[j--]){
return 0;
}
}
return 1;
}
char *getPalindrome(char str[]){
if (isPalindrome(str)){
return "Is_Palindrome";
} else {
return "Not_Palindrome";
}
}
int howManySubstrings(char subStr[], char str[]){
int numSubs=0;
int lastEqualPosition = 0;
int i;
for(i=0;i<strlen(str);i++){
if (subStr[lastEqualPosition+1]==' '){
numSubs++;
lastEqualPosition=0;
}
if(subStr[lastEqualPosition]==str[i]){
lastEqualPosition++;
} else {
lastEqualPosition=0;
}
}
return numSubs;
}
void checkSubstringPalindrome(char subStr[], char iStringData[][50], char oStringData[][50], int nrIOfileLines){
int i,j;
int numSubstrings;
for (i=0;i<nrIOfileLines;i++){
strcpy(oStringData[i], iStringData[i]);
numSubstrings=howManySubstrings(subStr, iStringData[i]);
oStringData[i][strlen(iStringData[i])]='t';
oStringData[i][strlen(iStringData[i])+1]=numSubstrings+'0';
oStringData[i][strlen(iStringData[i])+2]='t';
char *palindromeStatus=getPalindrome(iStringData[i]);
for(j=0;j<strlen(palindromeStatus);j++){
oStringData[i][strlen(iStringData[i])+3+j]=palindromeStatus[j];
}
oStringData[i][strlen(iStringData[i])+3+strlen(palindromeStatus)]='n';
oStringData[i][strlen(iStringData[i])+3+strlen(palindromeStatus)+1]=' ';
}
}
void readFile(char filename[], char twoDimArr[][50]){
FILE *file;
if ((file = openFile(filename)) == NULL){
return 1; // @suppress("Unused return value")
}
char c;
int firstIndex=0;
int secondIndex=0;
while((c=fgetc(file))!=EOF){
if(c!='n') {
twoDimArr[firstIndex][secondIndex]=c;
secondIndex++;
} else {
twoDimArr[firstIndex][secondIndex]=' ';
secondIndex=0;
firstIndex++;
}
}
fclose(file);
}
int countFileLines(char filename[]){
FILE *file;
int numLines=0;
if ((file = openFile(filename)) == NULL){
return 1;
}
char c;
while((c=fgetc(file))!=EOF){
if(c=='n'){
numLines++;
}
}
fclose(file);
return numLines;
}
int main()
{
char iStrings[]="iStrings.txt";
char subStrings[]="subStrings.txt";
//count number of lines in each input file to initialize the arrays
int iStringsNumLines=countFileLines(iStrings);
int subStringsNumLines=countFileLines(subStrings);
//initialize arrays
char iStringsArr[iStringsNumLines][50];
char subStringsArr[subStringsNumLines][50];
char oStringsArr[iStringsNumLines][50];
//load data from files to arrays
readFile(iStrings,iStringsArr);
readFile(subStrings,subStringsArr);
int i;
for (i=0; i<subStringsNumLines; i++){
checkSubstringPalindrome(subStringsArr[i], iStringsArr, oStringsArr, iStringsNumLines);
writeFile("oStrings.txt", subStringsArr[i], oStringsArr, iStringsNumLines);
}
return 0;
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team