0% found this document useful (0 votes)
28 views

Strings C

The document discusses various functions in C for working with strings including scanf(), fgets(), puts(), strlen(), strcpy(), and functions to convert strings to lowercase and uppercase. It provides examples of using these functions to read user input as strings, pass strings to functions, get the length of strings, copy strings, and convert case without built-in functions. User-defined functions are demonstrated for calculating string length, copying strings, and changing case.

Uploaded by

soham242004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Strings C

The document discusses various functions in C for working with strings including scanf(), fgets(), puts(), strlen(), strcpy(), and functions to convert strings to lowercase and uppercase. It provides examples of using these functions to read user input as strings, pass strings to functions, get the length of strings, copy strings, and convert case without built-in functions. User-defined functions are demonstrated for calculating string length, copying strings, and changing case.

Uploaded by

soham242004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Example 1: scanf() to read a string

#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}

How to read a line of text?

You can use the fgets() function to read a line of string. And, you can
use puts() to display the string.

Example 2: fgets() and puts()

#include <stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
fgets(name, sizeof(name), stdin); // read string
printf("Name: ");
puts(name); // display string
return 0;
}

Passing Strings to Functions

Strings can be passed to a function in a similar way as arrays.


Example 3: Passing string to a Function

#include <stdio.h>
void displayString(char str[]);

int main()
{
char str[50];
printf("Enter string: ");
fgets(str, sizeof(str), stdin);
displayString(str); // Passing string to a function.
return 0;
}
void displayString(char str[])
{
printf("String Output: ");
puts(str);
}

C String Length: strlen() function


The strlen() function returns the length of the given string. It doesn't count null character '\0'.

#include<stdio.h>

#include <string.h>

int main()

char ch[20]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};

printf("Length of string is: %d",strlen(ch));

return 0;

}
C Copy String: strcpy()
The strcpy(destination, source) function copies the source string in destination.

#include<stdio.h>

#include <string.h>

int main()

char ch[20]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};

char ch2[20];

strcpy(ch2,ch);
printf("Value of second string is: %s",ch2);

return 0;

C example to calculate string length using User


Defined function
#include <stdio.h>

/*function to return length of the string*/

int stringLength(char*);
int main()
{
char str[100]={0};
int length;

printf("Enter any string: ");


scanf("%s",str);

/*call the function*/


length=stringLength(str);

printf("String length is : %d\n",length);

return 0;
}
/*function definition...*/
int stringLength(char* txt)
{
int i=0,count=0;

while(txt[i++]!='\0'){
count+=1;
}

return count;
}

program to copy one string to another using user


defined functions
#include <stdio.h>

/********************************************************
* function name :stringCpy
* Parameter :s1,s2 : string
* Description : copies string s2 into s1
********************************************************/

void stringCpy(char* s1,char* s2);


int main()
{
char str1[100],str2[100];

printf("Enter string 1: ");


scanf("%s",str1);//read string with spaces

stringCpy(str2,str1);

printf("String 1: %s \nString 2: %s\n",str1,str2);


return 0;
}

/******** function definition *******/


void stringCpy(char* s1,char* s2)
{
int i=0;
while(s2[i]!='\0')
{
s1[i]=s2[i];
i++;
}
s1[i]='\0'; /*string terminates by NULL*/
}

Program to convert string into lowercase and


uppercase without using library function in C

#include <stdio.h>

/********************************************************

* function name :stringLwr, stringUpr

* Parameter :character pointer s

* Description

stringLwr - converts string into lower case

stringUpr - converts string into upper case

********************************************************/

void stringLwr(char *s);

void stringUpr(char *s);

int main()

char str[100];

printf("Enter any string : ");

scanf("%s",str);//read string with spaces

stringLwr(str);

printf("String after stringLwr : %s\n",str);


stringUpr(str);

printf("String after stringUpr : %s\n",str);

return 0;

/******** function definition *******/

void stringLwr(char *s)

int i=0;

while(s[i]!='\0')

if(s[i]>='A' && s[i]<='Z')

s[i]=s[i]+32;

++i;

void stringUpr(char *s)

int i=0;

while(s[i]!='\0')

if(s[i]>='a' && s[i]<='z'){

s[i]=s[i]-32;

++i;

You might also like