How To Declare A String?: Strings
How To Declare A String?: Strings
When the compiler encounters a sequence of characters enclosed in the double quotation
marks, it appends a null character \0 at the end by default.
1. char s[5];
1. #include <stdio.h>
2. int main()
3. {
4. char name[20];
5. printf("Enter name: ");
6. scanf("%s", name);
7. printf("Your name is %s.", name);
8. return 0;
9. }
use the fgets() function to read a line of string. And, you can use puts() to display the string.
1. #include <stdio.h>
2. int main()
3. {
4. char name[30];
5. printf("Enter name: ");
6. fgets(name, sizeof(name), stdin); // read string
7. printf("Name: ");
8. puts(name); // display string
9. return 0;
10. }
Predefined functions:
1. #include <stdio.h>
2. #include <string.h>
3. int main()
4. {
5. char a[20]="Program";
6. char b[20]={'P','r','o','g','r','a','m','\0'};
7. char c[20];
8.
9. printf("Enter string: ");
10. gets(c);
11.
12. printf("Length of string a = %d \n",strlen(a));
13.
14. //calculates the length of string before null charcter.
15. printf("Length of string b = %d \n",strlen(b));
16. printf("Length of string c = %d \n",strlen(c));
17.
18. return 0;
19. }
The strcpy() function copies the string pointed by source (including the null character) to the
character array destination.
This function returns character array destination.
1. #include <stdio.h>
2. #include <string.h>
3.
4. int main()
5. {
6. char str1[10]= "awesome";
7. char str2[10];
8. char str3[10];
9.
10. strcpy(str2, str1);
11. strcpy(str3, "well");
12. puts(str2);
13. puts(str3);
14.
15. return 0;
16. }
C strcmp() Prototype
int strcmp (const char* str1, const char* str2);
The strcmp() compares two strings character by character. If the first character of two strings
are equal, next character of two strings are compared. This continues until the corresponding
characters of two strings are different or a null character '\0' is reached.
negative if the ASCII value of first unmatched character is less than second.
positive
integer if the ASCII value of first unmatched character is greater than second.
1. #include <stdio.h>
2. #include <string.h>
3.
4. int main()
5. {
6. char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
7. int result;
8.
9. // comparing strings str1 and str2
10. result = strcmp(str1, str2);
11. printf("strcmp(str1, str2) = %d\n", result);
12.
13. // comparing strings str1 and str3
14. result = strcmp(str1, str3);
15. printf("strcmp(str1, str3) = %d\n", result);
16.
17. return 0;
18. }
_________________
C strcat() Prototype
char *strcat(char *dest, const char *src)
It takes two arguments, i.e, two strings or character arrays, and stores the resultant
concatenated string in the first string specified in the argument.
1. #include <stdio.h>
2. #include <string.h>
3. int main()
4. {
5. char str1[] = "This is ", str2[] = "C pro";
6.
7. //concatenates str1 and str2 and resultant string is stored in str1.
8. strcat(str1,str2);
9.
10. puts(str1);
11. puts(str2);
12.
13. return 0;
14. }
15. #include <stdio.h>
16. #include <string.h>
17. int main()
18. {
19. /* String Declaration*/
20. char nickname[20];
21.
22. printf("Enter your Nick name:");
23.
24. /* I am reading the input string and storing it in nickname
25. * Array name alone works as a base address of array so
26. * we can use nickname instead of &nickname here
27. */
28. scanf("%s", nickname);
29.
30. /*Displaying String*/
31. printf("%s",nickname);
32.
33. return 0;
34. }
35. ______________________
36.
37. #include <stdio.h>
38. #include <string.h>
39. int main()
40. {
41. /* String Declaration*/
42. char nickname[20];
43.
44. /* Console display using puts */
45. puts("Enter your Nick name:");
46.
47. /*Input using gets*/
48. gets(nickname);
49.
50. puts(nickname);
51.
52. return 0;
53. }
Q1. C Program to Find the Frequency of Characters in a String
1. int main()
2. {
3. char str[1000], ch;
4. int i, frequency = 0;
5.
6. printf("Enter a string: ");
7. gets(str);
8.
9. printf("Enter a character to find the frequency: ");
10. scanf("%c",&ch);
11.
12. for(i = 0; str[i] != '\0'; ++i)
13. {
14. if(ch == str[i])
15. ++frequency;
16. }
17.
18. printf("Frequency of %c = %d", ch, frequency);
19.
20. return 0;
21. }
Q2. Program to count vowels, consonants
1. int main()
2. {
3. char line[150];
4. int i, vowels, consonants, digits, spaces;
5.
6. vowels = consonants = digits = spaces = 0;
7.
8. printf("Enter a line of string: ");
9. scanf("%[^\n]", line);
10.
11. for(i=0; line[i]!='\0'; ++i)
12. {
13. if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
14. line[i]=='o' || line[i]=='u' || line[i]=='A' ||
15. line[i]=='E' || line[i]=='I' || line[i]=='O' ||
16. line[i]=='U')
17. {
18. ++vowels;
19. }
20. else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
21. {
22. ++consonants;
23. }
24. else if(line[i]>='0' && line[i]<='9')
25. {
26. ++digits;
27. }
28. else if (line[i]==' ')
29. {
30. ++spaces;
31. }
32. }
33.
34. printf("Vowels: %d",vowels);
35. printf("\nConsonants: %d",consonants);
36. printf("\nDigits: %d",digits);
37. printf("\nWhite spaces: %d", spaces);
38.
39. return 0;
40. }
Example:
#include <stdio.h>
int main(void) {
// variables
char
displayString(message);
return 0;
Another Way:
#include <stdio.h>
int main(void) {
// variables
char
displayString(message);
return 0;
int i = 0;
printf("String: ");
printf("%c", str[i]);
i++;
printf("\n");
Example:
void displayCities(char str[][50], int rows);
#include <stdio.h>
int main(void) {
// variables
char
cities[][50] = {
"Bangalore",
"Chennai",
"Kolkata",
"Mumbai",
"New Delhi"
};
int rows = 5;
displayCities(cities, rows);
return 0;
// variables
int r, i;
printf("Cities:\n");
i = 0;
while(str[r][i] != '\0') {
printf("%c", str[r][i]);
i++;
printf("\n");