#include <stdio.h> // for printf(), scanf(), getchar()
#define LENGTH 100 // max word length
void squeeze(char[], int); // remove all occurrences of a char from a string
int main()
{
char word[LENGTH];
char c;
printf("Type a word: ");
scanf("%s", word); // type the word,
getchar(); // then press Enter
printf("Type a char to remove from \"%s\": ", word);
scanf("%c", &c); // without getchar(), c would be '\n' (Enter)
printf("squeeze(%s, %c): ", word, c);
squeeze(word,c);
printf("%s\n", word);
return 0;
}
void squeeze(char s[], int c) // remove all c from s[]
{
int i, j;
for (i = j = 0; s[i] != '\0'; i++)
{
if (s[i] != c) {s[j++] = s[i];}
}
s[j] = '\0';
}
/*
gcc squeeze.c -o squeeze
./squeeze
Type a word: abracadabra
Type a char to remove from "abracadabra": a
squeeze(abracadabra, a): brcdbr
./squeeze
Type a word: abracadabra
Type a char to remove from "abracadabra": b
squeeze(abracadabra, b): aracadara
./squeeze
Type a word: abracadabra
Type a char to remove from "abracadabra": v
squeeze(abracadabra, v): abracadabra
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf(), scanf()
#define LENGTH 100 // max word length
int strLen(char[]); // strlen() is declared in string.h, included by stdio.h
int main()
{
char word[LENGTH];
printf("Type a word: ");
scanf("%s", word);
printf("Length of \"%s\" is %d\n", word, strLen(word));
return 0;
}
int strLen(char s[]) // returns the length of string s[]
{
int i = 0;
while (s[i] != '\0')
{
i++;
}
return i;
}
/*
gcc strlen.c -o strlen
./strlen
Type a word: abracadabra
Length of "abracadabra" is 11
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf(), scanf()
#define LENGTH 100 // max word length
void strCat(char[], char[]); // strcat() is declared in string.h, included by stdio.h
int main()
{
char s[LENGTH*2], t[LENGTH];
printf("Type a word: ");
scanf("%s", s);
printf("Type another word: ");
scanf("%s", t);
printf("strCat(%s, %s): ", s, t);
strCat(s,t); // only s[] changes
printf("%s\n", s);
return 0;
}
// concatenate t[] to the end of s[]; s[] must be big enough
void strCat(char s[], char t[])
{
int i = 0, j = 0;
while (s[i] != '\0') // find the end of s[]
{i++;}
while ((s[i++] = t[j++]) != '\0')
{} // copy ending '\0', then exit
}
/*
gcc strcat.c -o strcat
./strcat
Type a word: hocus-
Type another word: pocus
strCat(hocus-, pocus): hocus-pocus
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 2-4. Write an alternate version of squeeze(s1,s2) that deletes each character in s1 that matches any character in the string s2.
#include <stdio.h> // for printf(), scanf()
#define LENGTH 100 // max word length
void squeezes(char s[], char t[]); // remove all of t[] from s[]
int main()
{
char s1[LENGTH], s2[LENGTH];
printf("Type a word: ");
scanf("%s", s1);
printf("Type another word: ");
scanf("%s", s2);
printf("squeezes(%s, %s): ", s1, s2);
squeezes(s1,s2);
printf("%s\n", s1); // s2 unchanged
return 0;
}
void squeezes(char s[], char t[]) // remove all of t[] from s[]
{
int i, j, k;
for (i = 0; t[i] != '\0'; i++)
{
for (j = k = 0; s[j] != '\0'; j++)
{
if (s[j] != t[i]) {s[k++] = s[j];}
}
s[k] = '\0';
}
}
/*
gcc squeezes1.c -o squeezes1
./squeezes1
Type a word: abracadabra
Type another word: bro
squeezes(abracadabra, bro): aacadaa
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf(), scanf()
#define LENGTH 100 // max word length
void squeeze(char[], int); // remove all occurrences of a char from a string
void squeezes(char s[], char t[]); // remove all of t[] from s[]
int main()
{
char s1[LENGTH], s2[LENGTH];
printf("Type a word: ");
scanf("%s", s1);
printf("Type another word: ");
scanf("%s", s2);
printf("squeezes(%s, %s): ", s1, s2);
squeezes(s1,s2);
printf("%s\n", s1); // s2 unchanged
return 0;
}
void squeeze(char s[], int c) // remove all c from s[]
{
int i, j;
for (i = j = 0; s[i] != '\0'; i++)
{
if (s[i] != c) {s[j++] = s[i];}
}
s[j] = '\0';
}
void squeezes(char s[], char t[]) // remove all of t[] from s[]
{
int i;
for (i = 0; t[i] != '\0'; i++)
{
squeeze(s, t[i]);
}
}
/*
gcc squeezes2.c -o squeezes2
./squeezes2
Type a word: abracadabra
Type another word: bro
squeezes(abracadabra, bro): aacadaa
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 2-5. Write the function any(s1,s2), which returns the first location in the string s1 where any character from the string s2 occurs, or -1 if s1 contains no characters from s2. (The standard library function strpbrk() does the same job, but returns a pointer to the location.)
#include <stdio.h> // for printf(), scanf()
#define LENGTH 100 // max word length
int any(char s[], char t[]); // return first location of a char of t[] in string s[] or -1
int main()
{
int pos;
char s1[LENGTH], s2[LENGTH];
printf("Type a word: ");
scanf("%s", s1);
printf("Type another word: ");
scanf("%s", s2);
printf("The first location of any of \"%s\" in \"%s\": ", s2, s1);
pos = any(s1, s2);
printf("%d", pos);
if (pos < 0) {printf(" (not found)\n");}
else {printf(" (%c)\n", s1[pos]);}
return 0;
}
int any(char s[], char t[]) // return first location of a char of t[] in string s[] or -1
{
int i, j;
for (i = 0; t[i] != '\0'; i++)
{
for (j = 0; s[j] != '\0'; j++)
{
if (s[j] == t[i]) {return j;}
}
}
return -1; // not found
}
/*
gcc anychar.c -o anychar
./anychar
Type a word: abracadabra
Type another word: bro
The first location of any of "bro" in "abracadabra": 1 (b)
./anychar
Type a word: abracadabra
Type another word: a
The first location of any of "a" in "abracadabra": 0 (a)
./anychar
Type a word: abracadabra
Type another word: vip
The first location of any of "vip" in "abracadabra": -1 (not found)
./anychar
Type a word: abracadabra
Type another word: hocus
The first location of any of "hocus" in "abracadabra": 4 (c)
*/