Structured Programming Language
Strings in C
Mohammad Imam Hossain,
Lecturer, CSE, UIU
String
• A String is an array of characters terminated
by a special symbol named as null character
(represented as ‘0’ )
• Null character is also named as string-
termination character and used to identify
the length of the string.
• Logical illustration of a string consisting of n
characters:
X[0] X[1] X[2] … … … X[n-1] ‘0’
Defining a String
char string_name[expression];
• string_name  user chosen name for the
string
• expression integer expression indicates
the number of elements in the string,
including the null character
Sample Code:
char name[50];
String vs Char Array
Operations string char array
declaration char name[50]; ///max 49 characters char name[50]; ///max 50 characters
initialization char name[50]=“SPL";
or,
char name[50]={'S','P','L','0'};
or,
char name[50]={'S','P','L'};
char name[50]={'S','P','L'};
Reading a String
string char array
char str[50];
scanf("%s", str);
Or,
gets(str);
Or,
scanf("%[^n]", str);
char str[50];
int sz;
printf("Enter the size: ");
scanf("%d",&sz);
int i;
for(i=0;i<sz;i++){
scanf("%c",&str[i]);
}
Writing a String
string char array
char str[50];
printf("String : %sn",str);
Or,
puts(str);
char str[50];
for(i=0;i<sz;i++){
printf("%c",str[i]);
}
printf("n");
String I/O character wise
char str[50];
///reading string character wise
int i=0;
do{
scanf("%c",&str[i]);
i=i+1;
}while(str[i-1]!='n');
str[i-1]='0';
///writing string character wise
i=0;
while(str[i]!='0'){
printf("%c",str[i]);
i=i+1;
}
printf("n");
String Processing
• Way 1 using some predefined functions with help of
<string.h> header file
• Way 2 processing all characters individually
String Length
Character wise processing Header file <string.h>
#include <stdio.h>
#include <string.h>
int main()
{
char str[50];
gets(str);
int i=0;
while(str[i]!='0'){
i++;
}
int length=i;
printf("Length %dn",length);
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char str[50];
gets(str);
int length=strlen(str);
printf("Length %dn",length);
return 0;
}
String Concatenation
Character wise processing Header file <string.h>
#include <stdio.h>
#include <string.h>
int main()
{
char dest_string[50],src_string[30];
gets(dest_string);
gets(src_string);
printf("%sn",dest_string);
int len1=strlen(dest_string);
int i=0;
while(src_string[i]!='0'){
dest_string[len1+i]=src_string[i];
i++;
}
dest_string[len1+i]='0';
printf("%sn",dest_string);
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char dest_string[50],src_string[30];
gets(dest_string);
gets(src_string);
printf("%sn",dest_string);
strcat(dest_string,src_string);
printf("%sn",dest_string);
return 0;
}
String Copy
Character wise processing Header file <string.h>
#include <stdio.h>
#include <string.h>
int main()
{
char dest_string[50]="hello...",src_string[30];
gets(src_string);///world
int i=0;
while(src_string[i]!='0'){
dest_string[i]=src_string[i];
i++;
}
dest_string[i]='0';
printf("%s",dest_string);///world
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char dest_string[50]="hello…",src_string[30];
gets(src_string);///world
strcpy(dest_string,src_string);
printf("%s",dest_string);///world
return 0;
}
String Compare
Character wise processing Header file <string.h>
#include <stdio.h>
#include <string.h>
int main()
{
char str1[50],str2[50];
gets(str1);
gets(str2);
int i=0;
while(str1[i]!='0' && str2[i]!='0'){
i++;
}
int cmp=str1[i]-str2[i];
if(cmp<0){
printf("str2 is greatern");
}
else if(cmp>0){
printf("str1 is greatern");
}
else if(cmp==0) {
printf("Same stringn");
}
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char str1[50],str2[50];
gets(str1);
gets(str2);
if(strcmp(str1,str2)<0){
printf("str2 is greatern");
}
else if(strcmp(str1,str2)>0){
printf("str1 is greatern");
}
else if(strcmp(str1,str2)==0) {
printf("Same stringn");
}
return 0;
}
LowerCase Conversion
Character wise processing Header file <string.h>
#include <stdio.h>
#include <string.h>
int main()
{
char str[50];
gets(str);
printf("%sn",str);
int i=0;
while(str[i]!='0'){
if(str[i]>='A' && str[i]<='Z'){
str[i]='a'+str[i]-'A';
}
i++;
}
printf("%sn",str);
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char str[50];
gets(str);
printf("%sn",str);
strlwr(str);
printf("%sn",str);
return 0;
}
UpperCase Conversion
Character wise processing Header file <string.h>
#include <stdio.h>
#include <string.h>
int main()
{
char str[50];
gets(str);
printf("%sn",str);
strupr(str);
printf("%sn",str);
return 0;
}
Reversing a String
Character wise processing Header file <string.h>
#include <stdio.h>
#include <string.h>
int main()
{
char str[50];
gets(str);
printf("%sn",str);
int len=strlen(str);
int first=0;
int last=len-1;
while(first<last){
int tmp=str[first];
str[first]=str[last];
str[last]=tmp;
first++;
last--;
}
printf("%sn",str);
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char str[50];
gets(str);
printf("%sn",str);
strrev(str);
printf("%sn",str);
return 0;
}
String to integer
Character wise processing Header file <string.h>
#include <stdio.h>
#include <string.h>
int main()
{
char str[50];
gets(str);
printf("string: %sn",str);
int digit=0;
int i=0;
while(str[i]!='0'){
digit=digit*10+str[i]-'0';
i++;
}
printf("integer: %dn",digit);
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char str[50];
gets(str);
printf("string: %sn",str);
int digit=atoi(str);
printf("integer: %dn",digit);
return 0;
}
Integer to string
Character wise processing Header file <string.h>
#include <stdio.h>
#include <string.h>
void int_to_str(int val,char str[], int base)
{
int index=0;
while(val!=0)
{
int rem=val%base;
if(rem>9)
{
rem='a'+rem-10;
str[index]=rem;
}
else str[index]='0'+rem;
val=val/base;
index++;
}
str[index]='0';
strrev(str);
return ;
}
#include <stdio.h>
#include <string.h>
int main()
{
char str[50];
int num;
scanf("%d",&num);
printf("integer: %dn",num);
itoa(num,str,10);///value, string name, base of
conversion system
printf("decimal string: %sn",str);
itoa(num,str,2);///converting into binary
printf("binary string: %sn",str);
itoa(num,str,8);///converting into octal
printf("octal string: %sn",str);
itoa(num,str,16);///converting into hexadecimal
printf("hexadecimal string: %sn",str);
return 0;
}
Integer to string
Character wise processing Header file <string.h>
#include <stdio.h>
#include <string.h>
void int_to_str(int val,char str[], int base)
{
int index=0;
while(val!=0)
{
int rem=val%base;
if(rem>9)
{
rem='a'+rem-10;
str[index]=rem;
}
else str[index]='0'+rem;
val=val/base;
index++;
}
str[index]='0';
strrev(str);
return ;
}
#include <stdio.h>
#include <string.h>
int main()
{
char str[50];
int num;
scanf("%d",&num);
printf("integer: %dn",num);
itoa(num,str,10);///value, string name, base of
conversion system
printf("decimal string: %sn",str);
itoa(num,str,2);///converting into binary
printf("binary string: %sn",str);
itoa(num,str,8);///converting into octal
printf("octal string: %sn",str);
itoa(num,str,16);///converting into hexadecimal
printf("hexadecimal string: %sn",str);
return 0;
}
int main()
{
char str[50];
int num,base;
printf("Enter number and base to convert: ");
scanf("%d %d",&num,&base);
printf("integer: %dn",num);
int_to_str(num,str,base);
printf("string: %s",str);
return 0;
}
References
• Programming with C, Schaum’s outline, 3rd edition,
chapter 10
• https://www.tutorialspoint.com/cprogramming/c_str
ings.htm

SPL 13 | Character Array(String) in C

  • 1.
    Structured Programming Language Stringsin C Mohammad Imam Hossain, Lecturer, CSE, UIU
  • 2.
    String • A Stringis an array of characters terminated by a special symbol named as null character (represented as ‘0’ ) • Null character is also named as string- termination character and used to identify the length of the string. • Logical illustration of a string consisting of n characters: X[0] X[1] X[2] … … … X[n-1] ‘0’
  • 3.
    Defining a String charstring_name[expression]; • string_name  user chosen name for the string • expression integer expression indicates the number of elements in the string, including the null character Sample Code: char name[50];
  • 4.
    String vs CharArray Operations string char array declaration char name[50]; ///max 49 characters char name[50]; ///max 50 characters initialization char name[50]=“SPL"; or, char name[50]={'S','P','L','0'}; or, char name[50]={'S','P','L'}; char name[50]={'S','P','L'};
  • 5.
    Reading a String stringchar array char str[50]; scanf("%s", str); Or, gets(str); Or, scanf("%[^n]", str); char str[50]; int sz; printf("Enter the size: "); scanf("%d",&sz); int i; for(i=0;i<sz;i++){ scanf("%c",&str[i]); }
  • 6.
    Writing a String stringchar array char str[50]; printf("String : %sn",str); Or, puts(str); char str[50]; for(i=0;i<sz;i++){ printf("%c",str[i]); } printf("n");
  • 7.
    String I/O characterwise char str[50]; ///reading string character wise int i=0; do{ scanf("%c",&str[i]); i=i+1; }while(str[i-1]!='n'); str[i-1]='0'; ///writing string character wise i=0; while(str[i]!='0'){ printf("%c",str[i]); i=i+1; } printf("n");
  • 8.
    String Processing • Way1 using some predefined functions with help of <string.h> header file • Way 2 processing all characters individually
  • 9.
    String Length Character wiseprocessing Header file <string.h> #include <stdio.h> #include <string.h> int main() { char str[50]; gets(str); int i=0; while(str[i]!='0'){ i++; } int length=i; printf("Length %dn",length); return 0; } #include <stdio.h> #include <string.h> int main() { char str[50]; gets(str); int length=strlen(str); printf("Length %dn",length); return 0; }
  • 10.
    String Concatenation Character wiseprocessing Header file <string.h> #include <stdio.h> #include <string.h> int main() { char dest_string[50],src_string[30]; gets(dest_string); gets(src_string); printf("%sn",dest_string); int len1=strlen(dest_string); int i=0; while(src_string[i]!='0'){ dest_string[len1+i]=src_string[i]; i++; } dest_string[len1+i]='0'; printf("%sn",dest_string); return 0; } #include <stdio.h> #include <string.h> int main() { char dest_string[50],src_string[30]; gets(dest_string); gets(src_string); printf("%sn",dest_string); strcat(dest_string,src_string); printf("%sn",dest_string); return 0; }
  • 11.
    String Copy Character wiseprocessing Header file <string.h> #include <stdio.h> #include <string.h> int main() { char dest_string[50]="hello...",src_string[30]; gets(src_string);///world int i=0; while(src_string[i]!='0'){ dest_string[i]=src_string[i]; i++; } dest_string[i]='0'; printf("%s",dest_string);///world return 0; } #include <stdio.h> #include <string.h> int main() { char dest_string[50]="hello…",src_string[30]; gets(src_string);///world strcpy(dest_string,src_string); printf("%s",dest_string);///world return 0; }
  • 12.
    String Compare Character wiseprocessing Header file <string.h> #include <stdio.h> #include <string.h> int main() { char str1[50],str2[50]; gets(str1); gets(str2); int i=0; while(str1[i]!='0' && str2[i]!='0'){ i++; } int cmp=str1[i]-str2[i]; if(cmp<0){ printf("str2 is greatern"); } else if(cmp>0){ printf("str1 is greatern"); } else if(cmp==0) { printf("Same stringn"); } return 0; } #include <stdio.h> #include <string.h> int main() { char str1[50],str2[50]; gets(str1); gets(str2); if(strcmp(str1,str2)<0){ printf("str2 is greatern"); } else if(strcmp(str1,str2)>0){ printf("str1 is greatern"); } else if(strcmp(str1,str2)==0) { printf("Same stringn"); } return 0; }
  • 13.
    LowerCase Conversion Character wiseprocessing Header file <string.h> #include <stdio.h> #include <string.h> int main() { char str[50]; gets(str); printf("%sn",str); int i=0; while(str[i]!='0'){ if(str[i]>='A' && str[i]<='Z'){ str[i]='a'+str[i]-'A'; } i++; } printf("%sn",str); return 0; } #include <stdio.h> #include <string.h> int main() { char str[50]; gets(str); printf("%sn",str); strlwr(str); printf("%sn",str); return 0; }
  • 14.
    UpperCase Conversion Character wiseprocessing Header file <string.h> #include <stdio.h> #include <string.h> int main() { char str[50]; gets(str); printf("%sn",str); strupr(str); printf("%sn",str); return 0; }
  • 15.
    Reversing a String Characterwise processing Header file <string.h> #include <stdio.h> #include <string.h> int main() { char str[50]; gets(str); printf("%sn",str); int len=strlen(str); int first=0; int last=len-1; while(first<last){ int tmp=str[first]; str[first]=str[last]; str[last]=tmp; first++; last--; } printf("%sn",str); return 0; } #include <stdio.h> #include <string.h> int main() { char str[50]; gets(str); printf("%sn",str); strrev(str); printf("%sn",str); return 0; }
  • 16.
    String to integer Characterwise processing Header file <string.h> #include <stdio.h> #include <string.h> int main() { char str[50]; gets(str); printf("string: %sn",str); int digit=0; int i=0; while(str[i]!='0'){ digit=digit*10+str[i]-'0'; i++; } printf("integer: %dn",digit); return 0; } #include <stdio.h> #include <string.h> int main() { char str[50]; gets(str); printf("string: %sn",str); int digit=atoi(str); printf("integer: %dn",digit); return 0; }
  • 17.
    Integer to string Characterwise processing Header file <string.h> #include <stdio.h> #include <string.h> void int_to_str(int val,char str[], int base) { int index=0; while(val!=0) { int rem=val%base; if(rem>9) { rem='a'+rem-10; str[index]=rem; } else str[index]='0'+rem; val=val/base; index++; } str[index]='0'; strrev(str); return ; } #include <stdio.h> #include <string.h> int main() { char str[50]; int num; scanf("%d",&num); printf("integer: %dn",num); itoa(num,str,10);///value, string name, base of conversion system printf("decimal string: %sn",str); itoa(num,str,2);///converting into binary printf("binary string: %sn",str); itoa(num,str,8);///converting into octal printf("octal string: %sn",str); itoa(num,str,16);///converting into hexadecimal printf("hexadecimal string: %sn",str); return 0; }
  • 18.
    Integer to string Characterwise processing Header file <string.h> #include <stdio.h> #include <string.h> void int_to_str(int val,char str[], int base) { int index=0; while(val!=0) { int rem=val%base; if(rem>9) { rem='a'+rem-10; str[index]=rem; } else str[index]='0'+rem; val=val/base; index++; } str[index]='0'; strrev(str); return ; } #include <stdio.h> #include <string.h> int main() { char str[50]; int num; scanf("%d",&num); printf("integer: %dn",num); itoa(num,str,10);///value, string name, base of conversion system printf("decimal string: %sn",str); itoa(num,str,2);///converting into binary printf("binary string: %sn",str); itoa(num,str,8);///converting into octal printf("octal string: %sn",str); itoa(num,str,16);///converting into hexadecimal printf("hexadecimal string: %sn",str); return 0; } int main() { char str[50]; int num,base; printf("Enter number and base to convert: "); scanf("%d %d",&num,&base); printf("integer: %dn",num); int_to_str(num,str,base); printf("string: %s",str); return 0; }
  • 19.
    References • Programming withC, Schaum’s outline, 3rd edition, chapter 10 • https://www.tutorialspoint.com/cprogramming/c_str ings.htm