Você está na página 1de 6

C Programming, String

C Programming
String
Topic Covered: 1. Declaring and Initializing String 2. Printing String 3. Reading String 4. Arithmetic O eration on character! ". atoi# $ %&nction '. String handling %&nction a. !trlen# $ (. !trc )# $ c. !trcat# $ d. !trcm # $ *. Arra) o% String Declaring and Initializing String: String is a array of character. A !tring +aria(le i! a +aria(le declared a! arra) o% character. ,he general %ormat o% declaring !tring i!char string_name [size];

.ere !tring/name i! a +aria(le name and !ize indicate! )o&r !tring contain! ho0 man) character mean! length o% !tring. 1hen the com iler a!!ign! the !tring to the character arra), it a&tomaticall) &t the n&ll character #23$ at the end o% !tring. So the !ize o% !tring !ho&ld (e ma4im&m n&m(er o% character! l&! one. e.g. char city[10]; .ere the +aria(le name i! cit) and !ize i! 13. Initialization o% !tring +aria(le can (e done () t0o 0a)!. One i! a !im le method 0hich 0e are &!ing in initialization o% arra). char string_name[size] = { list of the character in string within single cot}; Another method i! in 0hich 0e a!!ign the !tring directl) 0ithin do&(le cot. char string_name[size] = String; e.g. char name[10] = { !" e!" l!" l!" o!" #0!}; In %ir!t ca!e 0e ha+e to &t n&ll character at the end o% !tring e4 licitl). 1hile () &!ing !econd method 0e can do directl). char name[10] = ello; 1e can al!o initialize the !tring +aria(le 0itho&t ! eci%)ing the !ize o% +aria(le. char name[ ] = ello; Printing String: ,o rint the !tring 0e are &!ing !ame rint% %&nction (&t 0e ha+e to ! eci%) the o&t &t %ormat a! 5! $rintf%&s" string_name'; 6) 000.!hare(ca.com Share 6CA.Com A lace to learn and !hare all a(o&t 6CA

C Programming, String

e.g.

char name[(0] = Swaminarayan; $rintf%&s#n" name'; A(o+e !tatement rint the !tring name. O&t &t i! 7S0aminara)an8.

1e can al!o %ormat the o&t &t o% !tring a! &nder&w)$s .ere 0 indicate! the total 0idth, indicate! no. o% character! )o& 0ant to rint, and ! indicate 0e are rinting !tring +al&e. e.g. char str[100]= ello; $rintf%&10)*s" str'; .ere in o&t &t 0e are getting total 0idth913 (&t the o&t &t character i! onl) 3 that i! 7.el8 !ince 0e ha+e +al&e 93. 1e can al!o &!e &t! %&nction to rint the !tring $+ts%string_name'; .ere there i! no need to &t the ne0 line character a%ter the !tring +aria(le (&t %&nction it!el% a&tomaticall) &t the ne0 line a%ter the !tring. e.g. $+ts%name'; It 0ill rint the +al&e o% +aria(le name 0ith ne0 line at end. Reading String: ,o read 0e can &!e !ame !can% %&nctionscanf%&s" string_name'; 6&t here 0e ha+e to not &t : o erator (eca&!e !tring +aria(le a&tomaticall) determine it! addre!!. 1e )o& com lete the reading o% !tring )o&r com &ter &t n&ll character a&tomaticall) at the end o% !tring. ,here i! one limitation o% !tring reading () &!ing !im le !can% %&nction. It cannot read 0hite ! ace #! ace, ta(, ne0 line$. ,hat mean! )o& cannot read entire !tring 7;a) S0aminara)an8 () &!ing !im le %&nction !ince it contain! 0hite ! ace. ,here are man) !ol&tion! to read entire !tring 0ith 0hite ! ace1. 1e ha+e to read the entire !tring character () character in loo & to 0e are not entering ne0 line character. At the end o% !tring 0e ha+e to e4 licitl) &t the n&ll character to indicate the end o% !tring. char c" name[100]; int i=0; ,o { c=getchar% ' ; name[i] = c; i--; } while %c .= #n!'; name[i] = #0!; 6) 000.!hare(ca.com Share 6CA.Com A lace to learn and !hare all a(o&t 6CA

C Programming, String

2.

1e ha+e to &!e get! %&nction 0hich read the !tring & to ne0 line. char name[100]; gets% name ' ; // this will rea, the string +$ to new line

3.

1e can &!e !ame !can% %&nction 0ith %ormatting to read the !tring. scanf%&[0#n]" name'; .ere 0e ha+e &!ed 5<=character>8 %ormat mean! )o&r com &ter read the !tring & to )o& are not entering the ne0 line character.

Arithmetic operations on characters: C allo0! the mani &lation o% character! !imilar to n&m(er. 1hene+er character con!tant i! &!ed in e4 re!!ion o% arithmetic o eration! it i! a&tomaticall) con+erted into integer +al&e () !)!tem. ,he integer +al&e de end! on the local character !et &!ed () !)!tem. ?enerall) it i! ASCII character !et. e.g. char c = a!; $rintf%&," c'; .ere 0e ha+e a!!igned a to character +aria(le c. 1e are &!ing the 5d %ormat in rint% !tatement mean! 0e are getting e@&i+alent integer n&m(er a!!igned () the !)!tem to a. In o&r ca!e it i! A* #ASCII +al&e o% a$ char c; c = z! 11; $rintf%&c" c'; .ere %ir!t !)!tem determine the integer e@&i+alent to z #122$ and !&(tract 1 %rom it #122B1$ and then a!!ign to c. #121 i! a!!igned to c$. In rint% () &!ing 5c 0e are getting character e@&i+alent to 121 mean! 0e get ) in o&t &t. int n=23; $rintf%&c" n'; .ere the character e@&i+alent to n&m(er '" i! determined #0hich i! A$ and that character 0e are getting in o&t &t. char c; $rintf%4nter yo+r character 5 '; scanf%&c) 6c'; if%c 7= 0! 66 c 8 = 9!' { $rintf%:o+r character is ,igit'; } .ere 0e are reading the +al&e o% character c. A%ter getting it 0e checC 0hether the integer e@&i+alent o% it i! in (et0een integer e@&i+alent to 3 and A. And it i! there then )o&r character i! digit and 0e are getting o&t &t 7Do&r character i! digit8.

atoi( !"nction: ,hi! %&nction i! &!ed to con+ert !tring into n&m(er. ,he %ormat o% thi! %&nction i!int atoi%string';

1e ha+e to a!! the !tring a! arg&ment and %&nction ret&rn! integer e@&i+alent to !tring. e.g. int a; 6) 000.!hare(ca.com Share 6CA.Com A lace to learn and !hare all a(o&t 6CA

C Programming, String a = atoi%1(*'; .ere a%ter e4ec&ting a(o+e !tatement +aria(le a contain! n&m(er 123. int a; a = atoi%1(* ;3'; I% )o& in!ert an) non digit character in (et0een )o&r n&m(er !tring the con+er!ion i! tr&ncated 0hen )o&r com &ter enco&nter non digit character. .ere +al&e o% a i!123 onl). int a; a = atoi%<1(*'; I% )o&r !tring contain! %ir!t character a! nonBdigit character then the ret&rn +al&e i! zero. .ere the o&t &t o% a i! 3. String handling !"nction: In C it i! not allo0 to a!!ign one !tring +al&e into another directl) () &!ing a!!ignment o erator. Al!o 0e cannot com are t0o !tring 0ith &!ing e@&al to o erator #9 9$. Similarl) the Eoining o% t0o !tring! cannot o!!i(le () &!ing !im le addition o erator #F$. Gor that &r o!e C ro+ide! !ome ! ecial %&nction. ,he!e all %&nction! are incl&ded into !tring.h header %ile. #$ strlen( !"nction: ,hi! %&nction i! &!ed to determine the length o% !tring. ,hat mean! thi! %&nction ret&rn integer n&m(er, )o&r !tring contain! ho0 man) character!. ,he %ormat o% the %&nction i!int strlen%string'; In %&nction 0e ha+e to a!! !tring +aria(le or !tring con!tant and %&nction ret&rn! integer +al&e 0hich indicate the length o% !tring. e.g. int l; char name[100]; l = strlen%name'; // =i>e the length of string

$rintf%?ength of string = &,#n" l'; %$ strcp&( !"nction: ,hi! %&nction i! &!ed to co ) or a!!ign the +al&e o% one !tring into another !tring. ,hi! %&nction contain! t0o arg&ment! and ret&rn! nothing. >oi, strc$y%,estination" so+rce'; G&nction ha! t0o arg&ment! one i! !o&rce %rom 0hich 0e ha+e to co ) the !tring and another i! de!tination into 0hich 0e ha+e to co ) the !tring. e.g. char str1[100] = ello; char str([100]; strc$y%str("str1'; // @o$y the >al+e of str1 into str(

.ere 0e ha+e t0o !tring +aria(le one i! !tr1 another i! !tr2. Gir!t +aria(le i! initialized () the +al&e 7.ello8 and !econd +aria(le contain! no %i4ed +al&e. 1hen 0e are &!ing !trc ) %&nction 0e ha+e to a!! !tr2 a! de!tination and !tr1 a! !o&rce. '$ strcat( !"nction: ,hi! %&nction i! &!ed to Eoin t0o !tring!. G&nction ha! t0o arg&ment! and ret&rn! nothing. >oi, strcat%,estination" so+ce'; 6) 000.!hare(ca.com Share 6CA.Com A lace to learn and !hare all a(o&t 6CA

C Programming, String

G&nction ha! one arg&ment de!tination another i! !o&rce and ret&rn! +oid. de!tination i! a !tring 0hich contain! the Eoined !tring a%ter e4ec&tion o% %&nction and !o&rce contain! the !tring 0hich 0e ha+e to Eoin 0ith the re+io&! +al&e o% de!tination (e%ore e4ec&tion o% %&nction. e.g. char str1[100] = ello; char str([100] = ow r +A; strcat %str1" '; strcat %str1" str('; // Boin s$ace as string to str1 // Boin str( to str1

$rintf%Che Doine, string is &s#n"str1'; In a(o+e e4am le %ir!t !tring +aria(le !tr1 i! initialized () 7.ello8 and !econd !tr2 i! initialized () 7.o0 r &H8. ,hen 0e call %&nction !trcat to Eoin the ! ace 0ith the !tr1. Io0 )o&r !tr1 +al&e i! 7.ello 7. ,hen again 0e call !trcat %&nction to Eoin !tr2 0ith the !tr1. So a%ter e4ec&tion !tr1 contain! the +al&e 7.ello .o0 r &H8 and !tr2 i! remain a! it i!. ($ strcmp( !"nction: ,hi! %&nction i! &!ed to com are t0o !tring!. It determine! 0hich !tring i! larger or !maller o&t o% t0o !tring!. ,he %ormat o% %&nction i!int strcm$ % string1" string(';

.ere one arg&ment i! !tring1 and another i! !tring2. 1e 0ant to com are t0o !tring!. ,he ret&rn +al&e i! integer. De ending on the ret&rn +al&e 0e can E&dge 0hich i! larger !tring and 0hich i! !maller !tring. I% !tring1 i! larger then the ret&rn +al&e i! o!iti+e, i% !tring1 i! !maller then ret&rn +al&e i! negati+e, and i% (oth !tring! are e@&al then ret&rn +al&e i! zero. string1 7 string( string1 8 string( string1 = string( e.g. char str1[100] = $Er; char str([100] = Fyz; int a; a = strcm$ % str1" str('; // @om$are str1 an, str( o+t$+t 7 0 o+t$+t 8 0 o+t$+t = 0

if %a 7 0 ' { $rintf%String1 is larger#n'; } else if%a 8 0 ' { $rintf%String1 is smaller #n'; } else { $rintf%Goth strings are eE+al#n'; } .ere !tring1 i! !maller then the !tring2 !o the o&t &t o% a i! negati+e +al&e and 0e get o&t &t 7String1 i! !maller8.

6) 000.!hare(ca.com

Share 6CA.Com A lace to learn and !hare all a(o&t 6CA

C Programming, String

Arra& o! String: 1hen 0e need the arra) o% the !tring then 0e ha+e to &!e t0oBdimen!ional arra) o% character!. ,he %ormat o% the arra) o% the !tring i!char string_name[array size][length of each string]; 1e ha+e to ! eci%) t0o dimen!ion!, one i! !ize o% the arra) and another i! the length o% each !tring. e.g. char name[10][100]; .ere 0e ha+e the arra) o% the !tring name 0hich !ize i! 13. ,hat mean! 0e ha+e 13 di%%erent name element!. ,he length o% the character i! 133 mean! each !tring element! can contain! ma4im&m 133 character!. ,o read the !tring or rint the !tring or mani &late the !tring element! 0e ha+e to &!e arra) !&(!cri t. e.g. scanf%&s" name[0]'; // Hea, first array string element scanf%&s" name[1]'; // Hea, secon, array string element $rintf%&s#n" name[0]'; $rintf%&s#n" name[1]'; // Irint first array string element // Irint secon, array string element

6) 000.!hare(ca.com

Share 6CA.Com A lace to learn and !hare all a(o&t 6CA

Você também pode gostar