Community Sites
Create your own community website and start earning today !
It's Free !
 
Communities Members BookmarksPolls Fresher Jobs Strange Photos Academic Projects New Member FAQ  



My Profile
Active Members
TodayLast 7 Days more...



Awards & Gifts
Online Exams

Fresher Jobs


Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian cities including Bangalore, Chennai, Hyderabad, Pune or Kochi

Resources


Find educational articles, blogs, discussion threads and other resources.

Colleges


Find details about any college in India or search for courses.

website counter



C Aptitude Questions


Posted Date: 05 Mar 2008    Resource Type: Articles/Knowledge Sharing    Category: Placement Papers

Posted By: Girish Patil       Member Level: Diamond
Rating:     Points: 5



Strings

In C language Strings are defined as an array of characters or a pointer to a portion of memory containing ASCII characters. A string in C is a sequence of zero or more characters followed by a NULL '\0' character:

It is important to preserve the NULL terminating character as it is how C defines and manages variable length strings. All the C standard library functions require this for successful operation.

All the string handling functions are prototyped in: string.h or stdio.h standard header file. So while using any string related function, don't forget to include either stdio.h or string.h. May be your compiler differs so please check before going ahead.

If you were to have an array of characters WITHOUT the null character as the last element, you'd have an ordinary character array, rather than a string constant.

String constants have double quote marks around them, and can be assigned to char pointers as shown below. Alternatively, you can assign a string constant to a char array - either with no size specified, or you can specify a size, but don't forget to leave a space for the null character!

char *string_1 = "Hello";
char string_2[] = "Hello";
char string_3[6] = "Hello";

Reading and Writing Strings:
One possible way to read in a string is by using scanf. However, the problem with this, is that if you were to enter a string which contains one or more spaces, scanf would finish reading when it reaches a space, or if return is pressed. As a result, the string would get cut off. So we could use the gets function

A gets takes just one argument - a char pointer, or the name of a char array, but don't forget to declare the array / pointer variable first! What's more, is that it automatically prints out a newline character, making the output a little neater.

A puts function is similar to gets function in the way that it takes one argument - a char pointer. This also automatically adds a newline character after printing out the string. Sometimes this can be a disadvantage, so printf could be used instead.

#include

int main() {
char array1[50];
char *array2;

printf("Now enter another string less than 50");
printf(" characters with spaces: \n");
gets(array1);

printf("\nYou entered: ");
puts(array1);

printf("\nTry entering a string less than 50");
printf(" characters, with spaces: \n");
scanf("%s", array2);

printf("\nYou entered: %s\n", array2);

return 0;
}

This will produce following result:

Now enter another string less than 50 characters with spaces:
hello world

You entered: hello world

Try entering a string less than 50 characters, with spaces:
hello world

You entered: hello


String Manipulation Functions:

char *strcpy (char *dest, char *src);
Copy src string into dest string.

char *strncpy(char *string1, char *string2, int n);
Copy first n characters of string2 to stringl .

int strcmp(char *string1, char *string2);
Compare string1 and string2 to determine alphabetic order.

int strncmp(char *string1, char *string2, int n);
Compare first n characters of two strings.

int strlen(char *string);
Determine the length of a string.

char *strcat(char *dest, const char *src);
Concatenate string src to the string dest.

char *strncat(char *dest, const char *src, int n);
Concatenate n characters from string src to the string dest.

char *strchr(char *string, int c);
Find first occurrence of character c in string.

char *strrchr(char *string, int c);
Find last occurrence of character c in string.

char *strstr(char *string2, char string*1);
Find first occurrence of string string1 in string2.

char *strtok(char *s, const char *delim) ;
Parse the string s into tokens using delim as delimiter.





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: C Aptitude Questions
Previous Resource: C Aptitude Questions
Return to Discussion Resource Index
Post New Resource
Category: Placement Papers


Post resources and earn money!
 
Related Resources



Watch TV Channels
  • Watch Asianet TV online
  • Kairali TV in Internet
  • Surya TV online
  • Amritha TV Channel

  • Contact Us    Privacy Policy    Terms Of Use   

    SpiderWorks Technologies Pvt Ltd. 2006 - 2007 All Rights Reserved.