0% found this document useful (0 votes)
14 views8 pages

Lecture11 C String Functions

The document provides an overview of various string functions in C programming, including input and output functions like gets() and puts(), as well as character functions such as getchar() and putchar(). It also details common string manipulation functions like strlen(), strcmp(), strcat(), and strcpy(), along with examples demonstrating their usage. Overall, it serves as a guide for handling strings effectively in C.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
14 views8 pages

Lecture11 C String Functions

The document provides an overview of various string functions in C programming, including input and output functions like gets() and puts(), as well as character functions such as getchar() and putchar(). It also details common string manipulation functions like strlen(), strcmp(), strcat(), and strcpy(), along with examples demonstrating their usage. Overall, it serves as a guide for handling strings effectively in C.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

C Programming:

string functions
String Input Function: gets()
 The gets() function is useful for interactive programs.
 It reads characters from the keyboard until it reaches a newline character
(\n).
 Example
#include <stdio.h>
#include <string.h>

int main(void)
{
char caName[100];

printf("Write a sentence. \n");


gets(caName);
printf("The sentence you wrote is:'%s'\n", caName);
}//main

© Prof Suvendi Rimer, UJ


String Output Function: puts()
 The puts() function requires an argument as a pointer to a string.
 The puts() function stops accepting characters to output when it encounters
the null character. When puts() finally finds the closing null character, it
replaces it with a newline character.
 Each string displayed by puts() function is placed on a new line.

#include <stdio.h>
#define STRDEF "I am a #defined string"
int main(void)
{
static char str1[] = "An array was initialised to
me.";

puts("I'm an argument to puts()");


puts(STRDEF);
puts(str1);
}//main

© Prof Suvendi Rimer, UJ


Character Functions: getchar() and putchar()
 The getchar() function takes no arguments and it returns the next character
from the input.
 After typing in a character you have to use carriage return to return the
character to the function call.
 The putchar() function prints its argument.
 These functions deal with characters: they are faster and more compact than
the more general scanf() and printf() functions.

#include <stdio.h>
int main (void)
{
char ch;

printf("Type in a character followed by carriage return\n");


ch = getchar(); //gets the first character you type in the keyboard
putchar(ch); //types the character on the screen
}//main

© Prof Suvendi Rimer, UJ


Other Common C String Functions
 size_t strlen(const char *str)
 size_t represents unsigned short
 It returns length of the string without including end character (terminating char
‘\0’).

 size_t sizeof(const char *str)


 size_t represents unsigned short
 It returns the total allocated size assigned to the array.

 int strcmp(const char *str1, const char *str2)


 It compares two strings and returns an integer value. If both the strings are
the same (equal) then this function return 0 otherwise if str1 < str2 OR str1 is
a substring of str2 then it returns a negative value. If str1 > str2 then it returns
a positive value.

© Prof Suvendi Rimer, UJ


Other Common C String Functions
 char * strcat(char *str1, char *str2)
 It concatenates two strings and returns the concatenated string.

 char * strcpy( char *str1, char *str2)


 It copies the string str2 into string str1, including the end character (terminator
char ‘\0’).

 char * strlwr( char *str) : converts a string to lowercase


 char * strupr( char *str) : converts a string to uppercase
 char * strrev( char *str) : reverses a string
 char * strchr( char *str) : finds first occurrence of given character in string
 char * strstrr( char *str) : finds first occurrence of given string in another string

© Prof Suvendi Rimer, UJ


Example: strlen()
#include <stdio.h>
#define NAME "John Doe"
int main (void)
{
int len;

len = strlen(NAME);
printf("The length of string %s is %d\n", NAME, len);
}//main

© Prof Suvendi Rimer, UJ


Example:
#include <stdio.h>
#define SIZE 100
#define ADDON "'s smell like morning dew"
#define ROSE "rose"
int main (void)
{
char cBuff[SIZE];
char cTmp[SIZE];
printf("What is your favourite flower?\n");
gets(cBuff);
strcpy(cTmp, cBuff);
strcat(cBuff, ADDON);
puts(cBuff);
if (strcmp(ROSE, cTmp) != 0)
{
printf("%s are not as nice as roses.\n", cTmp);
}
© Prof Suvendi Rimer, UJ
}//main

You might also like