Welcome to the forum 👋, Guest

To access the forum content and all our services, you must register or log in to the forum. Becoming a member of the forum is completely free.

  • PËRSHËNDETJE VIZITOR!

    Nëse ju shfaqet ky mesazh do të thotë se ju nuk jeni regjistruar akoma. Anëtarët e rregjistruar kanë privilegjin të marrin pjesë në tema të ndryshme si dhe të komunikojnë me anëtarët e tjerë. Bëhu pjesë e forumit Netedy.com duke u REGJISTRUAR këtu ose nëse ke një llogari KYCU. Komunikim alternative i ketij forumi me vajza dhe djem nga te gjithe trevat shqiptare? Hyr ne: CHAT SHQIP.

[C++] Stringat

centos

ⓃⒺⓉⒺⒹⓎ.ⒸⓄⓂ
ANETAR ✓
Regjistruar më
Dhj 13, 2004
Mesazhe
39,819
Code:
#include <stdio.h>
#include <stdlib.h>
 
/* String Length */
int str_len(char *string)
{
    char *count = string;
    while(*count) {count++;}
    return count - string;
}
/* String Append */
char* str_append(char* string, char* append) {
    char* newstring = NULL;
    size_t needed = snprintf(NULL, 0, "%s%s", string, append);
    newstring = malloc(needed);
    sprintf(newstring, "%s%s", string, append);
    return newstring;
}
/* String Append Char */
char* str_append_chr(char* string, char append) {
    char* newstring = NULL;
    size_t needed = snprintf(NULL, 0, "%s%c", string, append);
    newstring = malloc(needed);
    sprintf(newstring, "%s%c", string, append);
    return newstring;
}
/* String Equals */
int str_equals(char *equal1, char *eqaul2)
{
   while(*equal1==*eqaul2)
   {
      if ( *equal1 == '\0' || *eqaul2 == '\0' ){break;}
      equal1++;
      eqaul2++;
   }
   if(*eqaul1 == '\0' && *eqaul2 == '\0' ){return 0;}
   else {return -1};
}
/* String Replace */
char* str_replace(char* search, char* replace, char* subject) {
    char* newstring = "";
    int i = 0;
    for(i = 0; i < str_len(subject); i++) {
        if (subject[i] == search[0]) {
            int e = 0;
            char* calc = "";
            for(e = 0; e < str_len(search); e++) {
                if(subject[i+e] == search[e]) {
                    calc = str_append_chr(calc, search[e]);
                }
            }
            if (str_equals(search, calc) == 0) {
                newstring = str_append(newstring, replace);
                i = i + str_len (search)-1;
            }
            else {
                newstring = str_append_chr(newstring, subject[i]);
            }
        }
        else {
            newstring = str_append_chr(newstring, subject[i]);
        }
    }
    return newstring;
}
/* String Replace Maximal */
char* str_replace_max(char* search, char* replace, char* subject, int count) {
    char* newstring = "";
    int i = 0;
    for(i = 0; i < str_len(subject); i++) {
        if (subject[i] == search[0]) {
            int e = 0;
            char* calc = "";
            for(e = 0; e < str_len(search); e++) {
                if(subject[i+e] == search[e]) {
                    calc = str_append_chr(calc, search[e]);
                }
            }
            if (str_equals(search, calc) == 0) {
                if(count > 0) {
                    newstring = str_append(newstring, replace);
                    i = i + str_len (search)-1;
                    count = count - 1;
                }
                else {
                    newstring = str_append_chr(newstring, subject[i]);
                }
                
            }
            else {
                newstring = str_append_chr(newstring, subject[i]);
            }
        }
        else {
            newstring = str_append_chr(newstring, subject[i]);
        }
    }
    return newstring;
}
 

Postime të reja

Theme customization system

You can customize some areas of the forum theme from this menu.

Choose the color combination according to your taste

Select Day/Night mode

You can use it by choosing the day and night modes that suit your style or needs.

Welcome to the forum 👋, Guest

To access the forum content and all our services, you must register or log in to the forum. Becoming a member of the forum is completely free.