[Legacy Content] This page contains very old information, which might not be valid anymore, they are only kept here for legacy purposes.
if you have any inquiries, feel free to contact me! Click here
Created: Friday, 29 November 2013 Written by Ehab Eldeeb
1- Write a function to calculate the length of a String
2- Write a function that copies content of the string "src" to string "dest"
3- Write a function that copies n-characters from string S1 to another string S2
// Note that this is different from strcpy, it takes a defined number of characters, AND dest/source are inversed
4- Write a Boolean Function which compares two strings S1 and S2 [This is different from strcmp]
the result will be 0 if the two strings are the same
otherwise the result will equal 1
strcmp function returns 0 or 1 or -1 .. This function will only return 0 or 1 since it's boolean
strcmp(S1, S2)
if S1 comes alphabatically before S2, it returns -1
if S1 comes alphabatically after S2, it returns 1
if they are identical, it returns 0
5- Write a function that concatenate (joins) two input strings S1 and S2 into string S1
// These Functions are tested and proved to be working perfectly correct, as long as the array length permits that
// Written by Ehab Eldeeb// Function to calculate length of a string
int strlen(char x[]){
int length = 0;
while(x[length] != '\0')
length++;
return length;
}// Function that copies string "source" to string "dest"
void strcpy(char dest[], char source[]){
int i = 0;
while(source[i] != '\0'){
dest[i] = source[i];
i++;
}
dest[i] = '\0'; // Force the destination string to close
}// Function that copies n-characters from string S1 to another string S2
// Note that this is different from strcpy, it takes a defined number of characters, AND dest/source are inversed
void strcopy(char s1[], char s2[], int n){
int i;
for(i=0; i < n; i++)
s2[i] = s1[i];
s2[i] = '\0'; // Force the destination string to close
}// Boolean Function which compares two strings S1 and S2
// the result will be 0 if the two strings are the same
// otherwise the result will equal 1
int compare(char s1[], char s2[]){
int i=0;
while(s1[i] != '\0' || s2[i] != '\0'){
if(s1[i] == s2[i])
return 0;
else
return 1;
i++;
}
}int strcmp(char s1[],char s2[]){
int i =0;
while( s1[i]!='\0')
{
if( s2[i]=='\0')
return 1;
else if( s1[i]< s2[i])
return -1;
else if( s1[i]> s2[i])
return 1;
i++;
}
return 0;
}// Function that concatenate two input strings S1 and S2 in string S1
void strcat(char s1[], char s2[]){
int i=0, j=strlen(s1);
while(s2[i] != '\0'){
s1[j] = s2[i];
i++;
j++;
}
s1[j] = '\0'; // Force string to close
}