C Programming

10-Characters and Strings In C Programming

C Programming Lessons, Learn C Programming, Introduction to C Language, How to Program with C, Characters and Strings

A string can be assigned to a string of characters or a variable of type char* in declarations.

char color [] = “blue”;

const char* clrPtr = “blue”;

Both assign the string “blue” to a variable.

The first declaration creates a color array with 5 elements. The elements of this array are the characters ‘b’,’l’,’u’,’e’ and ‘\0’. The second declaration creates a pointer variable, clrPtr, that points to the string “blue” located somewhere in memory.

Mini Program

#include<stdio.h>
#include<ctype.h> //*

int main()
{
    printf("%s\n%s\n%s\n","isdigit: ", isdigit('8') ? "8 is a number" : "8 is not a number",
           isdigit('#') ? "# is a number" : "# is not a number");
}

*Include the <ctype.h> leader file in your program when using functions in character libraries.

mini program - isdigit
mini program – isdigit

Reading User’s String Input

We can get string input from the user with the scanf() function that we have mentioned in the previous topics.

#include <stdio.h>
int main()
{
    char title[20]; //A char variable of size 20

    printf("Enter title: ");
    scanf("%s", title);

    printf("\nThe title is - %s -", title);

    return 0;
}
string input
string input

* Note that the & sign is not used here after %s. because remember from previous topic that array names are used as pointers.

The scanf() function will take the first word of the entered text. Try above example with multi strings input. So how do we read a line of text?

Another Method: fgets() – puts()

#include <stdio.h>
int main()
{
    char title[20];
    printf("Enter Title: ");
    fgets(title, sizeof(title), stdin);  // read string
    printf("\nEntered Title is: ");
    puts(title);    // display string

    return 0;
}
fgets - puts
fgets – puts

*Similar to the fgets() function, the gets() function can also be used. The important difference is that gets() can accept input of any length, causing a buffer overflow error.

Functions and Strings

#include <stdio.h>

void printString(char txt[]);

int main()
{
    char txt[30];

    printf("Enter text: ");
    fgets(txt, sizeof(txt), stdin);

    printString(txt);     // Passing string to a function.

    return 0;
}
void printString(char txtfunc[])
{
    printf("\nText Display: ");
    puts(txtfunc);
}
functions and strings
functions and strings

Pointers and Strings

#include <stdio.h>

int main(void) {
  char Text[] = "electronca com";

  printf("%c", *Text);     // displays: e
  printf("%c", *(Text+4));   // displays: t
  printf("%c", *(Text+11));   // displays: c

  char *TxtPtr;

  TxtPtr = Text;
  printf("\n%c", *TxtPtr);     // displays: e
  printf("%c", *(TxtPtr+4));   // displays: t
  printf("%c", *(TxtPtr+11));   // displays: c
}
pointers and strings
pointers and strings

An Example

/* int getchar(void);   takes the character from
            standard input and returns it as an integer
   int putchar(int c);  prints the character in c
*/

#include<stdio.h>

int main()
{
    char c, sentence[80];
    int i=0;
    puts("Enter sentence: ");

    while ((c=getchar())!='\n')
    {
        sentence[i++]=c; //gets each character
                        //of the sentence increasing
                        //1 by 1 at every cycle
    }
    sentence[i]= '\0'; //add null at the end of the sentence
    puts("\nEntered Sentence: ");
    puts(sentence);

    return 0;
}
getchar - input character
getchar – input character

String Conversion Functions

Depending on the nature of the problems encountered, you may have to make changes to the string variables time to time. This process can be done manually or ready-made functions can be used to save time.

Functions                           Explanation

strcpy()                                copies a string to another one                 

strlen()                                calculates the length of a string

strcat()                                concatenates strings

strcmp()                              compares two strings

strrev()                                 prints the string in reverse

strlwr()                                converts strings to lowercase

strupr()                                converts strings to uppercase

*When using functions in the string library, be sure to include the <string.h> header file.

Example

#include<stdio.h>
#include<string.h>

int main()
{
    const char*s1 = "Happy new year! ";
    const char*s2 = "Happy new year! ";
    const char*s3 = "Happy Holidays! ";

//Compares the strings s1 and s2. It returns 0 if s1 and s2 are equal,
//positive if s1 is greater than s2, and negative if s2 is greater than s1.

    printf("%s%s\n%s%s\n%s%s\n\n%s%2d\n%s%2d\n%s%2d\n\n",
           "s1= ",s1, "s2= ",s2, "s3= ",s3, "strcmp(s1,s2)= ",strcmp(s1,s2),
           "strcmp(s1,s3)= ", strcmp(s1,s3), "strcmp(s3,s1)= ",strcmp(s3,s1) );

    printf("%s%2d\n%s%2d\n%s%2d\n", "strncmp(s1,s3,6)= ", strncmp(s1,s3,6),
           "strncmp(s1,s3,7)= ", strncmp(s1,s3,7), "strncmp(s3,s1,7)= ", strncmp(s3,s1,7));

    return 0;
}
string functions
string functions

Leave a Reply

Your email address will not be published. Required fields are marked *