C string functions 🔠

preview_player
Показать описание
C string functions tutorial example explained

#C #string #functions

int main(){

char string1[] = "Bro";
char string2[] = "Code";

strlwr(string1); // converts a string to lowercase
//strupr(string1); // converts a string to uppercase
//strcat(string1, string2); // appends string2 to end of string1
//strncat(string1, string2, 1); // appends n characters from string2 to string1
//strcpy(string1, string2); // copy string2 to string1
//strncpy(string1, string2, 2); // copy n characters of string2 to string1

//strset(string1, '?'); //sets all characters of a string to a given character
//strnset(string1, 'x', 1); //sets first n characters of a string to a given character
//strrev(string1); //reverses a string

//int result = strlen(string1); // returns string length as int
//int result = strcmp(string1, string2); // string compare all characters
//int result = strncmp(string1, string2, 1); // string compare n characters
//int result = strcmpi(string1, string1); // string compare all (ignore case)
//int result = strnicmp(string1, string1, 1); // string compare n characters (ignore case)

printf("%s", string1);

/*
if(result == 0)
{
printf("These strings are the same");
}
else
{
printf("These strings are not the same");
}
*/

return 0;
}
Рекомендации по теме
Комментарии
Автор

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

int main(){

char string1[] = "Bro";
char string2[] = "Code";

strlwr(string1); // converts a string to lowercase
//strupr(string1); // converts a string to uppercase
//strcat(string1, string2); // appends string2 to end of string1
//strncat(string1, string2, 1); // appends n characters from string2 to string1
//strcpy(string1, string2); // copy string2 to string1
//strncpy(string1, string2, 2); // copy n characters of string2 to string1

//strset(string1, '?'); //sets all characters of a string to a given character
//strnset(string1, 'x', 1); //sets first n characters of a string to a given character
//strrev(string1); //reverses a string

//int result = strlen(string1); // returns string length as int
//int result = strcmp(string1, string2); // string compare all characters
//int result = strncmp(string1, string2, 1); // string compare n characters
//int result = strcmpi(string1, string1); // string compare all (ignore case)
//int result = strnicmp(string1, string1, 1); // string compare n characters (ignore case)

printf("%s", string1);

/*
if(result == 0)
{
printf("These strings are the same");
}
else
{
printf("These strings are not the same");
}
*/

return 0;
}

BroCodez
Автор

I was to so some of the functions (like strlwr, strcmpi, and etc) but for some reason they didn't exist in the string.h library (it kept saying the function doesn't exist). some people suggest that I make these functions myself using other libraries; but is there a way to fix the issue i'm having?

xetycm.
Автор

How can strcat() make string1 bigger without malloc? And I thought that a string created using [] is constant.

nonamedelete
Автор

strlwr(string) and strupr(string) don't exist in standard c library string.h. you have to have a little fun and create it yourself, by using a loop that calculates the correspondent lower/uppercase char in the ASCII table and replacing it, or usinc ctype.h and tolower()/toupper() functions to convert cases. the same for strset(), strnset(), strrev(), strcmpi() and strnicmp()
not defining the size of the strings results in stack overflow/stack smashing and unexpected behaviour.
Examples for string to lowercase/uppercase (this doesn't accept string literals as arguments):

#include <ctype.h>
char* stringToUpper(char * string){
int i = 0;
while (string[i] != '\0'){
string[i] = (char)toupper(string[i]);
i++;
}
return string;
}

char* stringToLower(char * string){
int i = 0;
while (string[i] != '\0'){
string[i] = (char)tolower(string[i]);
i++;
}
return string;
}

QuimChaos
Автор

anyone knows why the result of my strcat(string1, string2) is BroCdee? im using linux mint distro

hamidabduljaabbar
Автор

Sir, strupr and strlwr both these functions aren't working even after including string header file. How to fix it

tejusc
Автор

im little bit confused everything here works besides strset, strnset and strrev vs isnt recognizing them. i believe theyre not included in string.h and stdio.h is there a fix to it ? im writing my exam next week and were only allowed to use libraries we got shown example: bool etc aint allowed.

delright
Автор

first comment im already a big fan of your channel ty for sharing useful information

toxiclucien
Автор

most of these functions aren't working on my mac. any idea how to fix this?

jerileiconcepcion
Автор

Broo😢 I came for string token where that at😭😭

unikneupane
Автор

how can we remember all these functions ?

mohammedisarezwani
Автор

bro code u didn't cover strlen function

LunarProjectND
Автор

hello sir the strings functions are not working on my MacBook Pro ! what do I do ?? please help me out !!!!

junaiid