I am currently trying to find the substring of a string but also allow the user to choose whether they want it to be case-sensitive or case-insensitive. I realised that it is only reading the first letter of the match word.. I am not sure how to read the entire substring. I have just started learning C!
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX_LENGTH 255
char text[MAX_LENGTH], pattern[MAX_LENGTH], caseSelect;
char casesensitive;
int main(void) {
printf("Text:\n");
//scanf("%4[^\n]s", text);
fgets(text, MAX_LENGTH, stdin);
//printf("%s", text);
printf("Length of string: %d\n", strlen(text));
printf("Match to:\n");
fgets(pattern, MAX_LENGTH, stdin);
printf("Should match be case-sensitive?\n");
scanf("%c", &caseSelect);
caseSelect = toupper(caseSelect);
if (caseSelect == 'N') {
for(int i = 0; pattern[i]; i ){
pattern[i] = tolower(pattern[i]);
}
for (int i = 0; i < strlen(pattern); i )
{
char *position_ptr = strchr(text, pattern[i]);
int position = (position_ptr == NULL ? -1 : position_ptr - text);
printf("Matches at position %d.\n", position);
break;
}
} else if (caseSelect == 'Y') {
for (int i = 0; i < strlen(pattern); i )
{
char *position_ptr = strchr(text, pattern[i]);
int position = (position_ptr == NULL ? -1 : position_ptr - text);
printf("Matches at position %d.\n", position);
break;
}
} else {
printf("No Matches.\n");
}
}
CodePudding user response:
Edit:
You have another problem (I mean, besides the fact that it is searching individually each char of the pattern, instead of searching for the pattern, which is solved by usage of strstr) in your code: you are asking for the pattern with fgets.
But fgets includes the \n
(Return) in the string. So, unless you are trying to match for the last word in your string, it won't work, since that '\n' won't be found.
You have to remove that \n
. For example with pattern[strlen(pattern)-1]='\0'
. Or using the scanf
you have in a comment, instead of fgets
at least for the pattern reading.
Former answer
That is what strstr
is made for
int ix;
if(caseSelect!='N') ix=strstr(text, pattern)-text;
else ix=strcasestr(text,pattern)-text;
printf("Matches at position %ld.\n", ix);
Note: strstr return the pointer to the char of text that contains what is in pattern. For example:
text = "one two three";
pattern = "two";
if text
is located at address 0x01234567
, then word "two" in in starts at address 0x0123456b
.
Then pointer arithmetic makes the subtraction between those two addresses = 4.
Complete code
#define _GNU_SOURCE
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX_LENGTH 255
char text[MAX_LENGTH], pattern[MAX_LENGTH], caseSelect;
char casesensitive;
int main(void) {
printf("Text:\n");
//scanf("%4[^\n]s", text);
fgets(text, MAX_LENGTH, stdin);
//printf("%s", text);
printf("Length of string: %ld\n", strlen(text));
printf("Match to:\n");
fgets(pattern, MAX_LENGTH, stdin);
pattern[strlen(pattern)-1]='\0';
printf("Should match be case-sensitive?\n");
scanf("%c", &caseSelect);
caseSelect = toupper(caseSelect);
int position=0;
if (caseSelect != 'N') position=strstr(text,pattern)-text;
else position=strcasestr(text, pattern)-text;
if(position>=0) printf("Matches at position %d.\n", position);
else printf("No Matches.\n");
}