15.3 – More about String

by subbu on August 7, 2014

Reading a line of text

Now we know how to read and write a string using scanf() and printf() functions but, there is a drawback in reading a string using scanf(). scanf() can’t read a line of text and store into a character array, only the first word is read and stored into the array.

#include<stdio.h>
int main()
{
char x[100];
printf("Enter a line of text:");
scanf("%s",x);
printf("The given text is : %s",x);
return 0;
}

Execution:
Enter a line of text: hello world
The given text is: hello

Here the space after the first word is treated as the end of string. Only “Hello” is accepted. There is another function called gets(), can be used to accept a line of text including with spaces. It is defined in the header file “stdio.h”

#include<stdio.h>
int main()
{
char x[100];
printf("Enter a line of text:");
gets(x);
printf("The given text is : %s",x);
return 0;
}

Execution:
Enter a line of text: hello world
The given text is: hello world

Specification: Accept a line of text from the keyboard and print the number of vowels, consonants, digits, space characters and special symbols.

Program:

#include<stdio.h>
int main()
{
char x[100];
char ch;
int i,vc=0,cc=0,dc=0,sp=0,ss=0;
printf("Enter a line of text:");
gets(x);                                    /* reading a line of text */
for(i=0;x[i]!='\0';i++)
{
 ch=x[i];                            /* fetching character by character */
 if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
  vc++;
 else if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
  vc++
 else if(ch>='b'&&ch<='z')
  cc++;
 else if(ch>='B'&&ch<='Z')
  cc++;
 else if(ch>='0'&&ch<='9')
  dc++;
 else if(ch==' ')
  sp++;
 else
  ss++;
}
printf("Number of vowels: %d\n",vc);
printf("Number of consonents: %d\n",vc);
printf("Number of digits: %d\n",vc);
printf("Number of spaces: %d\n",sp);
printf("Number of special symbols: %d\n",ss);
return 0;
}

Execution:
Enter a line of text: Test signal,123
Number of vowels: 3
Number of consonants: 3
Number of digits: 3
Number of spaces: 1
Number of special symbols: 1

Another way of printing a string using puts()

puts() is another function defined in “stdio.h”. It accepts the address of character array and prints the string on the screen but, the difference among the printf() and puts() is that puts() adds a next line character after printing the string

#include<stdio.h>
int main()
{
char x[]="rocking";
puts(x);
puts("Hello world");
return 0;
}

Output:
rocking
Hello world

Initializing a string

A string can be initialized either to a character array or a character pointer. Functionally there would be no difference but the way memory allocated is different.

#include<stdio.h>
int main()
{
char x[]="rocking";
char *y="rocking";
printf("%s",x);
printf("\n%s",y);
return 0;
}

Output:
rocking
rocking

char x[]="rocking";

reference to stringHere initializing a string while declaration of a character array.

char *y="rocking";

pointer to string

Here initializing a string to the character pointer. The C runtime automatically creates a character array and stores the string with the terminating character ‘\0’ and then the address of base element is assigned to character pointer.

#include<stdio.h>
int main()
{
char x[]="rocking";
char *y="rocking";
printf("String with character array: %s",x);
printf("\nString with character pointer: %s",y);
printf("\nSize of string with character array: %d",sizeof(x));
printf("\nSize of string with character pointer: %d",sizeof(y));
return 0;
}

Output:
String with character array: rocking
String with character pointer: rocking
Size of string with character array: 8
Size of string with character pointer: 2

The above program is demonstrating that a string can be initialized to an array or a character pointer but, sizeof(x) gives 8 that is the size of character array and the sizeof(y) gives 2 that is the size of character pointer.

Quiz

What would be the output of following program?

#include<stdio.h>
int main()
{
char x[]="rocking";
char *p=x;
char *q=p;
printf("%s",x);
printf("\n%s",p);
printf("\n%s",q);
return 0;
}
Show Answer

Output:
rocking
rocking
rocking

Multiple references to a string

Here x, p and q refers the same string hence prints the same

What would be the output of following program?

#include<stdio.h>
int main()
{
char *y=3+"rocking";
printf("%s",y);
return 0;
}
Show Answer

Output:
king

pointer arithmetic with string

Here the address of “rocking” is the base elements address (6232), adding 3 to base address would be the address of “k” which is stored in the character pointer. Hence the printf() function gets the address of “k”, prints the string from “k” until the end of character ‘\0’. So the output would be “king”

What would be output of following program?

#include<stdio.h>
int main()
{
printf(3+"rocking");
return 0;
}
Show Answer

Output:
king
Here the printf() gets the address of “k”, hence prints from “k” to the end of string that is “king”

Previous post:

Next post: