Să se scrie o funcție C care primind ca si parametru 2 șiruri de caractere returnează un număr negativ dacă primul sir este mai mic, 0 dacă cele 2 siruri sunt egale si un număr pozitiv dacă primul sir este mai mare decât al 2-lea. Comparațiile sunt lexicografice. (implementare pentru funcția strcmp)
#include<stdio.h>
#include<string.h>
int my_strcmp(char s[], char d[]) {
int ns = strlen(s);
int nd = strlen(d);
for (int i = 0; ns != 0 || nd != 0; i++, ns--, nd--) {
if (s[i] > d[i]) {
return 1;
}
if (s[i] < d[i]) {
return -1;
}
}
if (ns == 0 && nd == 0) {
return 0;
}
// adjust the result if one of the string was consumed
return (ns == 0) ? -1 : 1;
}
int main()
{
char astr[40];
char bstr[40];
puts ("Please enter a string of 40 characters or fewer.");
scanf ("%40[^\n]s", astr);
printf("a string = %s\n", astr);
puts ("Please enter a string of 40 characters or fewer.");
scanf ("%40[^\n]s", bstr);
printf("b string = %s\n", bstr);
printf("result = %d\n", my_strcmp(astr, bstr));
return 0;
}
Să se scrie o funcție C care primind ca și parametru 2 siruri de caractere, le concatenează cu obținerea noului sir in primul argument al funcției (implementare pentru funcția strcat).
#include<stdio.h>
#include<string.h>
char *my_strcat(char s[], char d[]) {
int ns = strlen(s);
int nd = strlen(d);
for (int i = 0; i <= nd; i++) {
s[ns++] = d[i];
}
return s;
}
int main()
{
char astr[80];
char bstr[40];
puts ("Please enter a string of 40 characters or fewer.");
scanf ("%40[^\n]s", astr);
printf("a string = %s\n", astr);
puts ("Please enter a string of 40 characters or fewer.");
scanf (" %40[^\n]s", bstr);
printf("b string = %s\n", bstr);
printf("result = %s\n", my_strcat(astr, bstr));
printf("result = %s\n", astr);
return 0;
}
Se citeste un text de la tastatură, terminat prin caracterul sfarsit de rand (enter). Să se scrie programul C care determină numărul de apariții ale fiecărei litere din șir – literele mici și mari se consideră impreună.
#include<stdio.h>
#include<string.h>
int LETTERS = 25;
int freq(char s[], int fq[]) {
int i;
int ns = strlen(s);
// init fq array
for (i = 0; i < LETTERS; i++) {
fq[i] = 0;
}
// iterate every chars from char array
for (i = 0; i < ns; i++) {
char c = s[i];
// convert to lower
if (c >= 'a' && c <= 'z') {
c -= 32;
}
// if is letter, increment the frecquency for specific letter
// shift the letter codes interval to zero based interval
if (c >= 'A' && c <= 'Z') {
fq[c - 'A'] += 1;
}
}
}
int main()
{
char astr[80];
int fq[LETTERS];
puts ("Please enter a string of 80 characters or fewer.");
scanf ("%80[^\n]s", astr);
freq(astr, fq);
for(int i = 0; i < LETTERS; i++) {
if (fq[i] > 0) {
printf("%c = %d\n", i + 'A', fq[i]);
}
}
return 0;
}
Să se scrie 2 funcții C care caută un caracter intr-un sir de caractere și returnează (ca și pointer) prima apariție a caracterului in sir, considerând faptul că se face cautarea de la stânga și de la dreapta (funcțiile strchr și strrchr)
#include<stdio.h>
#include<string.h>
char *my_strchr (char s[], char c) {
int ns = strlen(s);
for (int i = 0; i < ns; i++) {
if (s[i] == c) {
return s + i;
}
}
return NULL;
}
char *my_strrchr (char s[], char c) {
int ns = strlen(s);
for (int i = ns - 1; i >= 0; i--) {
if (s[i] == c) {
return s + i;
}
}
return NULL;
}
int main()
{
char astr[40];
char c;
char *spos;
puts ("Please enter a string of 40 characters or fewer.");
scanf ("%40[^\n]s", astr);
printf("a string = %s\n", astr);
puts ("Please enter a char.");
scanf (" %c", &c);
printf("c char = %c\n", c);
if (spos = my_strchr(astr, c)) {
printf("Char was found from left on pos %s\n", spos);
}
else {
printf("Char %c not found in %s\n", c, astr);
}
if (spos = my_strrchr(astr, c)) {
printf("Char was found from right on pos %s\n", spos);
}
else {
printf("Char %c not found in %s\n", c, astr);
}
return 0;
}
Să se scrie o funcție C care caută apariția unui sir de caractere intr-un sir de caractere sursă. Funcția returnează ca locul apariției in sirul sursă ca și pointer, sau null dacă nu identifică nici o apariție (funcția strstr)
#include<stdio.h>
#include<string.h>
#define TRUE 1
#define FALSE 0
char *my_strstr(char s[], char d[]) {
int ns = strlen(s);
int nd = strlen(d);
// if substring d is longer than destination return null
if (nd > ns) {
return NULL;
}
for (int i = 0; i <= ns - nd; i++) {
if (s[i] == d[0]) {
// search the rest
int match = TRUE;
for (int j = 1; j < nd; j++) {
if (s[i+j] != d[j]) {
match = FALSE;
break;
}
}
if (match) {
return s + i;
}
}
}
return NULL;
}
int main()
{
char astr[MAX];
char bstr[40];
puts ("Please enter a string of 40 characters or fewer.");
scanf ("%40[^\n]s", astr);
printf("a string = %s\n", astr);
puts ("Please enter a string of 40 characters or fewer.");
scanf ("%40[^\n]s", bstr);
printf("b string = %s\n", bstr);
printf("result = %s\n", my_strstr(astr, bstr));
return 0;
}