본문 바로가기
C 언어

C 언어 substring 함수 구현 방법

by treeCoder 2021. 11. 23.

C substring 구현방법이다.


#include <stdio.h>

// Function to implement substring function in C
char* substring(char *destination, const char *source, int beg, int n)
{
// extracts `n` characters from the source string starting from `beg` index
// and copy them into the destination string
while (n > 0)
{
*destination = *(source + beg);

destination++;
source++;
n--;
}

// null terminate destination string
*destination = '\0';

// return the destination string
return destination;
}

// Implement `substring()` function in C
int main()
{
char source[] = "Techie Delight – Ace the Technical Interviews";
char destination[25];

int start = 7;
int len = 7;

substring(destination, source, start, len);

printf("%s\n", destination);

return 0;
}

————**********—————

char *trim(char *s) {
char *ptr;
if (!s)
return NULL; // handle NULL string
if (!*s)
return s; // handle empty string
for (ptr = s + strlen(s) - 1; (ptr >= s) && isspace(*ptr); --ptr);
ptr[1] = '\0';
return s;
}

—————***********—————

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

char *trimString(char *str)
{
char *end;

while(isspace((unsigned char)*str)) str++;

if(*str == 0)
return str;

end = str + strlen(str) - 1;
while(end > str && isspace((unsigned char)*end)) end--;

end[1] = '\0';

return str;
}

int main(void) {
const char *str1 = " temporary string ";

printf("%s\n", str1);

char *tmp = strdup(str1);
printf("%s\n", trimString(tmp));

free(tmp);
exit(EXIT_SUCCESS);
}

———-*********————

// C program to search and replace
// all occurrences of a word with
// other word.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


// Function to replace a string with another
// string

char* replaceWord(const char* s, const char* oldW,

const char* newW)
{

char* result;

int i, cnt = 0;

int newWlen = strlen(newW);

int oldWlen = strlen(oldW);



// Counting the number of times old word

// occur in the string

for (i = 0; s[i] != '\0'; i++) {

if (strstr(&s[i], oldW) == &s[i]) {

cnt++;



// Jumping to index after the old word.

i += oldWlen - 1;

}

}



// Making new string of enough length

result = (char*)malloc(i + cnt * (newWlen - oldWlen) + 1);



i = 0;

while (*s) {

// compare the substring with the result

if (strstr(s, oldW) == s) {

strcpy(&result[i], newW);

i += newWlen;

s += oldWlen;

}

else

result[i++] = *s++;

}



result[i] = '\0';

return result;
}


// Driver Program

int main()
{

char str[] = "xxforxx xx for xx";

char c[] = "xx";

char d[] = "Geeks";



char* result = NULL;



// oldW string

printf("Old string: %s\n", str);



result = replaceWord(str, c, d);

printf("New String: %s\n", result);


free(result);

return 0;
}

————-******—————-

int endswith(const char* withwhat, const char* what)
{
int l1 = strlen(withwhat);
int l2 = strlen(what);
if (l1 > l2)
return 0;

return strcmp(withwhat, what + (l2 - l1)) == 0;
}

****** strtok *****

for( j = 0, p = strtok(fileNames[i], "\\"); p!=NULL; j++ ) {
if(j == 2) sprintf(buf, "%s%s\\%03d-",buf, "C:\\tc1", strtol(p, NULL, 10));
if(j > 2) sprintf(buf, "%s%s\\",buf, p);
p = strtok(NULL, "\\");
}

—======******======——-

bool StartsWith(const char *a, const char *b)
{
if(strncmp(a, b, strlen(b)) == 0) return 1;
return 0;
}

######------------------------========================
char *strstrr(char *haystack, char *needle) {

char *p;
char *sav = NULL;

for( p = haystack; (p = strstr(p, needle)) != NULL; ) {
sav = p++;
}

return sav;
}

int main() {

char *p = strstrr("par1par2par3", "par");
if( p != NULL )
puts( p );
else
puts( "not found");
return 0;
}

######------------------------========================

void trim(char * s) {
    char * p = s;
    int l = strlen(p);
    if( l == 0 ) return;

    while(isspace(p[l - 1])) p[--l] = 0;
    while(* p && isspace(* p)) ++p, --l;

    memmove(s, p, l + 1);
}  

댓글