Created a new typedef and functions supporting it, making it user-friendly [specifically programmer-friendly].
What does the code do?
- It makes users [programmers] easy to deal with strings as most functions are there.
- Additionally, we can append, obtain substrings, replace string very very easily
- Also there is no specific limit to the string itself but most functions limit the str to GLOBAL_MAX_CHAR_LIMIT which is defined in mystring.h
Note: This code may contain bugs but I have fixed tons of them which I encountered. Also sstrreplace() is limited to have 7 chars in replacing strings -> I dont know why but if the length is greater than 7 wierd things happen <- I am in progress of finding its solution.
Also got to know that gets() is not safe so I used fgets() and removed '\n' in strinput()! All suggestions, feedbacks and bug-encounters are welcome!
Note: This code has been updated and has fixed bugs. updated version can be found here
The header file: mystring.h
#ifndef STRING_H_DEFINED
#define STRING_H_DEFINED
#define GLOBAL_MAX_CHAR_LIMIT 200
//dont need to include stdbool.h!
#define true 1
#define false 0
typedef int bool;
typedef struct{
char * _str;
}sstring;
typedef struct{
char _str[GLOBAL_MAX_CHAR_LIMIT];
}substr;
//functions declarations
sstring cstr2sstr(char _cstr[GLOBAL_MAX_CHAR_LIMIT]);
char sgetchar(sstring _str,int _pos);
substr strinput();
substr ssetchar(sstring _str,int _pos,char _ch);
substr sstrappend(sstring _str,char * _append);
substr ssubstr(sstring _str,int pos1,int len);
char * sstrval(sstring _str);
int cstrlen(const char * _str);
int sstrlen(sstring _str);
int sstrfind(int init,sstring _strvar,const char * _find);
sstring sstrreplace(sstring _str,char * _find,char * _repl);
#endif
mystring.c << to be included and compiled at once
#include "mystring.h"
#include<string.h>
#include<stdio.h>
char * sstrval(sstring _str){
return _str._str;
}
int cstrlen(const char * _str){
int i=0;
while(1>0){
if(_str[i]=='\0'){
return i;
}
i++;
}
}
int sstrlen(sstring _str){
return cstrlen(sstrval(_str));
}
substr sstrappend(sstring _str,char * _append){
int _len = sstrlen(_str),_len2=cstrlen(_append);
char temp[_len+_len2];
for(int i=0;i<_len+_len2;i++){
temp[i] = _str._str[i];
if(i>=_len){
temp[i] = _append[i-_len];
}
}
substr ret;
for(int i=0;i<cstrlen(temp);i++){
ret._str[i] = temp[i];
}
ret._str[_len+_len2] = '\0';
return ret;
}
int sstrfind(int init,sstring _strvar,const char * _find){
const char * _str;
_str = _strvar._str;
int _len1 = cstrlen(_str);
int _len2 = cstrlen(_find);
int matching = 0;
//some wierd conditions check [user, are u fooling the function ?]
if(_len2>_len1||init<0||init>_len1-1||_len2==0){
return -1;
}
//the main finder
for(int i=init;i<_len1+1;i++){
if(_str[i]==_find[0]){
for(int z=0;z<_len2+1;z++){
if(matching==_len2){
return i;
}
else if(_str[i+z]==_find[z]){
matching+=1;
}
else{
matching=0;
break;
}
}
}
}
return -1;
}
substr ssubstr(sstring _str,int pos1,int len){
substr self;
if(pos1<0||len<1||sstrlen(_str)<1||pos1+len>sstrlen(_str)){
return self;
}
char a[GLOBAL_MAX_CHAR_LIMIT];
for(int i=0;i<GLOBAL_MAX_CHAR_LIMIT;i++){
a[i] = '\0';
}
if(pos1==0){
for(int i=0;i<len;i++){
a[i] = _str._str[i];
}
}
for(int i=pos1;i<pos1+len;i++){
a[i-pos1] = _str._str[i];
}
substr b;
for(int i=0;i<GLOBAL_MAX_CHAR_LIMIT;i++){
b._str[i] = '\0';
}
for(int i=0;i<=GLOBAL_MAX_CHAR_LIMIT;i++){
if(i>GLOBAL_MAX_CHAR_LIMIT){
return self;
}
b._str[i] = a[i];
}
return b;
}
substr ssetchar(sstring _str,int _pos,char _ch){
substr tmp;
//nullify the string buffer
for(int i=0;i<GLOBAL_MAX_CHAR_LIMIT;i++){
tmp._str[i]='\0';
}
if(_pos<0){
return tmp;
}
//copy the string buffer and set the char of _pos as _ch
for(int i=0;i<sstrlen(_str);i++){
tmp._str[i]=_str._str[i];
if(i==_pos){
tmp._str[i]=_ch;
}
}
return tmp;
}
char sgetchar(sstring _str,int _pos){
char _ret;
_ret = '\0';
if(_pos<0){
return _ret;
}
_ret = _str._str[_pos];
return _ret;
}
substr strinput(){
substr temp;
fgets(temp._str,GLOBAL_MAX_CHAR_LIMIT,stdin);
temp._str[cstrlen(temp._str)-1] = '\0';
return temp;
}
sstring sstrreplace(sstring _str,char * _find,char * _repl){
//duplicate of _str
sstring _dup = _str;
//temp str's
sstring _tmp,_tmp2;
substr self,tmp;
int _lens = strlen(_str._str);
int _lenf = strlen(_find); int _lenr = strlen(_repl);
//current limit - idk why but if _lenr > 7 WIERD THINGS HAPPEN so return original string
if(_lenr>7){
return _dup;
}
int temp=0,nottoappend=0;
char tmpstr[GLOBAL_MAX_CHAR_LIMIT];
temp=sstrfind(0,_str,_find);
if(temp==-1){
return _dup;
}
int tmpint=0,num=1;
while(1>0){
temp = sstrfind(0,_dup,_find);
if(temp==-1){
//incase of char buffering
printf("%c\b \b",_dup._str[sstrlen(_dup)-1]);
return _dup;
}else{
if(temp==sstrlen(_dup)-1){
_dup._str = ssubstr(_dup,0,sstrlen(_dup)-1)._str;
_dup._str = sstrappend(_dup,_repl)._str;
//incase of char buffering
printf("%c\b \b",_dup._str[0]);
return _dup;
}
if(_lenr==0){
_tmp._str = ssubstr(_dup,0,temp)._str;
_tmp2._str = ssubstr(_dup,temp+_lenf,sstrlen(_dup)-temp-_lenf)._str;
_dup._str = sstrappend(_tmp,_tmp2._str)._str;
}else{
_tmp._str = ssubstr(_dup,0,temp)._str;
_tmp2._str = ssubstr(_dup,temp+_lenf,sstrlen(_dup)-temp-_lenf)._str;
_dup._str = sstrappend(_tmp,_repl)._str;
_dup._str = sstrappend(_dup,_tmp2._str)._str;
}
}
}
}
sstring cstr2sstr(char _cstr[GLOBAL_MAX_CHAR_LIMIT]){
sstring _ret;
_ret._str = "";
for(int i=0;i<cstrlen(_cstr);i++){
_ret._str = sstrappend(_ret," ")._str;
}
for(int i=0;i<cstrlen(_cstr);i++){
_ret._str = ssetchar(_ret,i,_cstr[i])._str;
//incase of char buffering
printf("%c\b%c\b \b",_cstr[i],_ret._str[i]);
}
return _ret;
}
exampleusage.c
#include<stdio.h>
#include "mystring.h"
int main(){
int len1,find;
sstring var1,var2,var3;
char chr;
printf("Enter your name: ");
//strinput!
var1._str = strinput()._str;
//length of string
len1 = sstrlen(var1);
printf("\nYour name's Length is: %d",len1);
//replacing all spaces with underscores
var2._str = sstrreplace(var1," ","_")._str;
printf("\nYour url might look like : '%s'",var2);
//with no spaces
var2._str = sstrreplace(var1," ","")._str;
printf("\nConJusted Name: %s",var2);
//appending " is a good person full of honesty and great behaviour!"
var2._str = sstrappend(var1," is a good person full of honesty and great behaviour!")._str;
printf("\nGood about you: %s",var2);
//finding space!
find = sstrfind(0,var1," ");
printf("\nFirst space in your name is in %d charachter",find);
//substrings!
find = sstrfind(0,var1," ");
var2._str = ssubstr(var1,0,find)._str;
printf("\nYour first name: %s",var2);
var2._str = ssubstr(var1,find+1,len1-find-1)._str;
printf("\nYour last name: %s",var2);
//setting chars!
//set first char to 'F'
var2._str = ssetchar(var1,0,'F')._str;
printf("\nYour name with first char as 'F': %s",var2);
//getting chars!
//get last char of name!
chr = sgetchar(var1,sstrlen(var1)-1);
printf("\nYour name's last char is '%c'",chr);
}