Strings inherit the properties of character arrays.
The same is true if the string is an argument to a function.
The latter can then modify the content of the string (unless the argument is declared with const).
However, thanks to the NULL character at the end of the string, it is not necessary to add a second parameter to specify the
size of the string.
The ucfirst() function below capitalizes the first character of the string str:
void ucfirst(char str[]) {
if (str[0]<'a' || str[0]>'z') return;
str[0] += 'A' - 'a';
}
The string is passed in parameters as an array of characters.
Write a function str_2_upper() that converts the string passed as parameter to uppercase.
For example:
Enter a text: Bonjour ! In capital letters: BONJOUR !
#include <stdio.h>
// Convert a string to uppercase
void str_2_upper(char s[]);
int main(void) {
char str[100];
// Ask for a string
printf ("Enter a text: ");
fgets(str, 100, stdin);
// Convert a text to uppercase
str_2_upper(str);
// Display the text in uppercase
// (fgets() keeps the line feed at the end of the string)
printf("In capital letters: %s", str);
return 0;
}
// Convert a string to uppercase
void str_2_upper(char s[]) {
int i;
for (i=0; s[i]!='\0'; i++)
if (s[i]>='a' && s[i]<='z')
s[i] += 'A' - 'a';
}
Write a function nbSpace() that returns the number of spaces in a string.
Here is the expected output of the program:
The text "To be or not to be..." contains 5 space(s).
#include <stdio.h>
// Return the number of spaces in the string
int nbSpace(const char s[]);
int main(void) {
char shakespear[] = "To be or not to be...";
// Display the number of spaces
printf ("The text \"%s\" contains %d space(s).\n", shakespear, nbSpace(shakespear));
return 0;
}
// Return the number of spaces in the string
int nbSpace(const char s[]) {
int i, n = 0;
for (i=0; s[i]!='\0'; i++)
if (s[i] == ' ')
n++;
return n;
}
Write a function int isCharIn(char s[], char needle) that returns the number of
occurrences needle in the string s. Here is the expected output of the program:
The text "To be or not to be..." contains 4 'o' and 2 'e'.
#include <stdio.h>
// Count the number of occurrences of needle in s
int isCharIn(const char s[], char needle);
int main(void) {
char shakespear[] = "To be or not to be...";
// Display the number of 'o' and 'e' in the string
printf("The text \"%s\" contains %d 'o' and %d 'e'.\n",
shakespear, isCharIn(shakespear, 'o'), isCharIn(shakespear, 'e'));
return 0;
}
// Count the number of occurrences of needle in s
int isCharIn(const char s[], char needle) {
int i, n = 0;
for (i=0; s[i]!='\0'; i++)
if (s[i] == needle)
n++;
return n;
}
Write a char palindrome() function that returns:
1 if the string s is a palindrome; 0 otherwise.Enter a word: radar radar is a palindrome.
Enter a word: pifometer pifometer is not a palindrome.
#include <stdio.h>
#include <string.h>
// Returns 1 if the string s is a palindrome
char palindrome(const char s[]);
int main(void) {
// 20 characters + the null character
char word[21];
// Ask for a word
printf("Enter a word: ");
scanf("%20s", word);
// if the word is a palindrome
if (palindrome(word))
printf("%s is a palindrome.\n", word);
else
printf("%s is not a palindrome.\n", word);
return 0;
}
// Returns 1 if the string s is a palindrome
char palindrome(const char s[]) {
int i, n = strlen(s);
// Compare the first and the last characters, then the second and the second to last...
for (i=0; i<n/2; i++)
if (s[i] != s[n-1-i])
return 0;
return 1;
}
To pass a string as a parameter in a function ...
A character string passed as a parameter in a function ...
char *strcpy(char *s1, const char *s2);
const, it cannot be modified by the function.
Try again...
A string passed into a function (without const) can be expanded ...