Showing posts with label C Programming - Strings. Show all posts
Showing posts with label C Programming - Strings. Show all posts

Thursday, March 17, 2016

Pointers in C

Pointers are often used to make C programming much easier and it helps programmers improve their program's efficiency. Not only that, by using the pointer's you can achieve unlimited amount of memory usage in your program, that’s cool right.

Definitions and Uses of Pointers

Pointers are the variables that hold an address of another variable of the same data type. The cool thing about the address of a variable is that, you'll then be able to go to that address and access the data stored in it. Pointers are basically used in the C in different ways. Some of them are:
  • To create dynamic and flexible data structures.
  • To pass and handle variable arguments passed to functions (Call by reference).
  • To retrieve information stored in list like arrays.

Pointer Variables

As I said earlier, pointers indicate the memory address of any kind of object i.e. It can be address of any primitive data types or structures or even functions. The variable which holds the address is called the pointer variable.

Wednesday, March 16, 2016

Commonly Used Functions

The most commonly used functions in the string library ie. Using <string.h> are:
  • strcat - concatenate two strings
  • strchr - string scanning operation
  • strcmp - compare two strings
  • strcpy - copy a string
  • strlen - get string length
  • strncat - concatenate one string with part of another
  • strncmp - compare parts of two strings
  • strrchr - string scanning operation
strcat:
char *strcat(char *s1, const char *s2);
s1 is a pointer to a string that will be modified. s2 will be copied to the end of s1. s2 is a pointer to a string that will be appended to the end of s1. It returns a pointer to s1 (where the resulting concatenated string resides).
strcpy:
char *strcpy(char *s1, const char *s2);
s1 is a pointer to the destination. s2 will be copied to the end of s1. It returns a pointer to s1 (where the resulting copied string resides).
strlen:
int strlen(char *s1);
s1 is a pointer to the string. It returns the length of the string excluding ‘\0’ character.
Example –
#include <stdio.h>
#include <string.h>

int main(int argc, const char * argv[])
{
/* Define a temporary variable */
char MyString[100];

/* Copy the first string into the variable */
strcpy(MyString, "a4");

/* Concatenate the following two strings to the end of the first one */
strcat(MyString, "academics ");
strcat(MyString, ".com");

/* Display the concatenated strings */
printf("%s\n", MyString);

/* Display the length of the string */
printf(“Length : %d\n”,strlen(MyString));

return 0;
}
Output –
a4academics.com
Length : 15
strchr :
char *strchr(const char *s, int c);
s is a string (terminated by a null character).c is the character to be found. It returns a pointer to the first occurrence of the character c within the string pointed to by s. If c isn't found, it returns a null pointer.
strcmp :
char *strcmp(const char *s1, const char *s2);
s1 and s2 a string (terminated by a null character). It returns the ASCII value difference of the first index of mismatched characters of the given string.
Example -
strcmp(“Samiran”,Saha);
It will return the difference between ASCII value of ‘m’ and ‘a’ ie. 12. Because first mismatch occurs at 3rd index.
strcmp(“Saha”,Saha);
It will return 0 because there is no mismatch in the given string.

Friday, July 11, 2014

C Programming - Strings

1. 
Which of the following function sets first n characters of a string to a given character?
A.strinit()B.strnset()
C.strset()D.strcset()

2. 
If the two strings are identical, then strcmp() function returns
A.-1B.1
C.0D.Yes
3. 
How will you print \n on the screen?
A.printf("\n");B.echo "\\n";
C.printf('\n');D.printf("\\n");
4. 
The library function used to find the last occurrence of a character in a string is
A.strnstr()B.laststr()
C.strrchr()D.strstr()
5. 
Which of the following function is used to find the first occurrence of a given string in another string?
A.strchr()B.strrchr()
C.strstr()D.strnset()
6. 
Which of the following function is more appropriate for reading in a multi-word string?
A.printf();B.scanf();
C.gets();D.puts();
7. 
Which of the following function is correct that finds the length of a string?
A.
int xstrlen(char *s)
{
    int length=0;
    while(*s!='\0')
    {    length++; s++; }
    return (length);
}
B.
int xstrlen(char s)
{
    int length=0;
    while(*s!='\0')
        length++; s++;
    return (length);
}
C.
int xstrlen(char *s)
{
    int length=0;
    while(*s!='\0')
        length++;
    return (length);
}
D.
int xstrlen(char *s)
{
    int length=0;
    while(*s!='\0')
        s++;
    return (length);
}