Exercise 5-6-6 (strings)

Chapter_5     Exercise_5-6     Exercise_5-6-5 Exercise_5-6-7     Exercise_5-7







Exercise 5-6     K&R, p. 107


Exercise 5-6. Rewrite appropriate programs from earlier chapters and exercises with pointers instead of array indexing. Good possibilities include getline() (Chapters 1 and 4), atoi(), itoa(), and their variants (Chapters 2, 3, and 4), reverse(), (Chapter 3), and strindex() and getop() (Chapter 4).




CONTENTS:     squeeze.c     squeezes  (squeezes1.c,  squeezes2.c)     anychar.c     reverse  (reverse1.c,  reverse2.c)




Note:  See also ch5-strlen, ch5-strcpy, ch5-strcmp, Exercise_5-3, Exercise_5-4, Exercise_5-5.




squeeze.c     K&R, p. 47  (ch2-squeeze)         download


#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
{
char *p;

for (p = s; *s != '\0'; s++)
{
if (*s != c) {*p++ = *s;}
}
*p = '\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
*/











squeezes  (squeezes1.c,  squeezes2.c)     K&R, p. 48  (Ex_2-4)




squeezes1.c         download


#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
{
char *p, *r;

for (p = s; *t != '\0'; t++)
{
for (r = s; *r != '\0'; r++) // r = s or r = p
{
if (*r != *t) {*p++ = *r;}
}
*p = '\0';
p = s; // reset p
}
}
/*
gcc squeezes1.c -o squeezes1
./squeezes1
Type a word: abracadabra
Type another word: a
squeezes(abracadabra, a): brcdbr

./squeezes1
Type a word: abracadabra
Type another word: bro
squeezes(abracadabra, bro): aacadaa

./squeezes1
Type a word: abracadabra
Type another word: abc
squeezes(abracadabra, abc): rdr

./squeezes1
Type a word: abracadabra
Type another word: hocus-pocus
squeezes(abracadabra, hocus-pocus): abraadabra

./squeezes1
Type a word: abracadabra
Type another word: ops
squeezes(abracadabra, ops): abracadabra
*/











squeezes2.c         download


#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
{
char *p;

for (p = s ; *s != '\0'; s++)
{
if (*s != c) {*p++ = *s;}
}
*p = '\0';
}

void squeezes(char *s, char *t) // remove all of t from s
{
for ( ; *t != '\0'; t++)
{
squeeze(s, *t);
}
}
/*
gcc squeezes2.c -o squeezes2
./squeezes2
Type a word: abracadabra
Type another word: a
squeezes(abracadabra, a): brcdbr

./squeezes2
Type a word: abracadabra
Type another word: abc
squeezes(abracadabra, abc): rdr

./squeezes2
Type a word: abracadabra
Type another word: bro
squeezes(abracadabra, bro): aacadaa

./squeezes2
Type a word: abracadabra
Type another word: hocus-pocus
squeezes(abracadabra, hocus-pocus): abraadabra

./squeezes2
Type a word: abracadabra
Type another word: mop
squeezes(abracadabra, mop): abracadabra
*/











anychar.c     K&R, p. 48  (Ex_2-5)         download


#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
{
char *p;

for ( ; *t != '\0'; t++)
{
for (p = s; *p != '\0'; p++)
{
if (*p == *t) {return p-s;}
}
}

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)

./anychar
Type a word: abracadabra
Type another word: rock
The first location of any of "rock" in "abracadabra": 2 (r)
*/











reverse  (reverse1.c,  reverse2.c)     K&R, p. 88  (Ex_4-13)




reverse1.c         download


#include <stdio.h> // for printf(), scanf()
#include <string.h> // for strlen()

#define SIZE 100 // array size

void reverse(char *s, int i, int j); // reverse a string from *(s+i) to *(s+j)

int main()
{
char array[SIZE];
int len; // array length

printf("Type a string:\t");
scanf("%s", array);

len = strlen(array);
reverse(array, 0, len-1);
printf("Reversed:\t%s\n", array);

return 0;
}

void reverse(char *s, int i, int j) // reverse a string from *(s+i) to *(s+j)
{
int temp;

if (i < j)
{
temp = *(s+i);
*(s+i) = *(s+j);
*(s+j) = temp;
i++;
j--;
reverse(s, i, j);
}
}
/*
gcc reverse1.c -o reverse1
./reverse1
Type a string: a
Reversed: a

./reverse1
Type a string: ana
Reversed: ana

./reverse1
Type a string: anna
Reversed: anna

./reverse1
Type a string: ab
Reversed: ba

./reverse1
Type a string: abc
Reversed: cba

./reverse1
Type a string: 123456
Reversed: 654321
*/











reverse2.c         download


#include <stdio.h> // for printf(), scanf()
#include <string.h> // for strlen()

#define SIZE 100 // array size

// reverse a string using a second string pointing to its end
void reverse(char *, char *);

int main()
{
char array[SIZE];
int len; // array length

printf("Type a string:\t");
scanf("%s", array);

len = strlen(array);
reverse(array, array+len-1);
printf("Reversed:\t%s\n", array);

return 0;
}

// reverse a string using a second string pointing to its end:
void reverse(char *s, char *t)
{
int temp;

if (s < t)
{
temp = *s;
*s = *t;
*t = temp;
s++;
t--;
reverse(s, t);
}
}
/*
gcc reverse2.c -o reverse2
./reverse2
Type a string: a
Reversed: a

./reverse2
Type a string: ana
Reversed: ana

./reverse2
Type a string: anna
Reversed: anna

./reverse2
Type a string: ab
Reversed: ba

./reverse2
Type a string: abc
Reversed: cba

./reverse2
Type a string: 123456
Reversed: 654321
*/









Chapter_5     Exercise_5-6     Exercise_5-6-5 BACK_TO_TOP Exercise_5-6-7     Exercise_5-7



Comments

Popular posts from this blog

Contents

Blogger Page Margins in Contempo