P. 1
String Operator

String Operator

|Views: 20|Likes:
Published by api-3705814

More info:

Published by: api-3705814 on Oct 14, 2008
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less





String operator: get length, compare and find a char

#include <stdio.h> #include <string.h> int main(void) { char s1[80], s2[80]; gets(s1); gets(s2); printf("lengths: %d %d\n", strle n(s1), strlen(s2)); if(!strcmp(s1, s2)) printf("The strings are equal\ n"); strcat(s1, s2); printf("%s\n", s1); strcpy(s1, "This is a test.\n"); printf(s1); if(strchr("hello", 'e')) printf("e is in hello\n"); if(strstr("hi there", "hi")) printf("found hi"); return 0; }

char str[10]; int i; printf("Enter a string: "); fgets(str, 10, stdin); /* remove newline, if presen t */ i = strlen(str) - 1; if(str[i] =='\n') str[i] = '\0'; printf("This is your string: % s", str); return 0;

Use fwrite and fread to save and read
#include <stdio.h> #include <stdlib.h> int main(void) { FILE *fp; int i; /* open file for output */ if((fp = fopen("my.txt", "wb"))==NUL L) {

Check string length and set string end
#include <stdio.h> #include <string.h> int main(void) {

printf("Cannot open file.\n"); exit(1); } i = 100; if(fwrite(&i, 2, 1, fp) != 1) { printf("Write error occurred.\n"); exit(1); } fclose(fp); /* open file for input */ if((fp = fopen("myfile", "rb"))==NULL ){ printf("Cannot open file.\n"); exit(1); } if(fread(&i, 2, 1, fp) != 1) { printf("Read error occurred.\n"); exit(1); } printf("i is %d", i); fclose(fp); return 0; }

printf("%s", str); } fclose(fp); return 0; }

Get a string from a stream: how to use fgets
#include <stdio.h> int main() { FILE *file; char string [100]; file = fopen ("my.txt" , "r"); if (file == NULL) perror ("Error reading file"); else { fgets (string , 100 , file); puts (string); fclose (file); } return 0; }

Get string from file
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { FILE *fp; char str[128]; if((fp = fopen(argv[ 1 ], "r"))== NULL) { printf("Cannot open file.\n"); exit(1); } while(!feof(fp)) { if(fgets(str, 126, fp))

Read formatted data from a stream: how to use fscanf
#include <stdio.h> int main () { char str[80]; float f; FILE *file;


file = fopen ("my.txt","w+"); fprintf (file, "%f %s", 3.14, "PI"); rewind (file); fscanf (file, "%f", &f); fscanf (file, "%s", str); fclose (file); printf (" %f and %s are added. \n",f,str); return 0;

student1.name = s1; student2.number = f; printf ("Name = %s \n", studen t1.name); printf ("Number = %f \n", stud ent2.number); }

strcmp: lexicographically compares two strings
//Declaration: int strcmp(const char *str1, con st char *str2); //Return: returns an integer based on the o utcome: < 0 : str1 is less than str2 0 : str1 is equal to str2 >0 : str1 is greater than str2 #include<stdio.h> #include<string.h> int main(void){ char s[80]; printf("Enter password: "); gets(s); if(strcmp(s, "pass")) { printf("Invalid Password\n"); return 0; } return 1; } /* Enter password: 34 Invalid Password */

. 3. 2. Reading Strings
The standard function fgets can be used to read a string from the keyboard. The general form of an fgets call is: fgets(name, sizeof(name), stdi n);

The arguments are: is the name of a name character array. indicates the maximum sizeof(name) number of characters to read. stdin is the file to read.

How to use struct
struct student { char name[30]; float number; } student1, student2; main () { struct student student3; char s1[30]; float f; scanf ("%s", s1); scanf ("%f", &f);

Read a line from the keyboard and reports its length.
#include <string.h> #include <stdio.h> int main() { char line[100]; /* Line we are looking at * printf("Enter a line: "); fgets(line, sizeof(line), stdin); printf("The length of the line is: %d\n", str len(line)); return (0); }


fgets - get a string from a stream

#include <stdio.h> char *fgets(char *restrict s, int n, FILE *restrict stream);

int main(void) { char text[100]; char substring[40]; printf("\nEnter the string to be searched(less than 100 characters):\n"); fgets(text, sizeof(text), stdin); printf("\nEnter the string sought (less than 40 characters ):\n"); fgets(substring, sizeof(substring), stdin); /* overwrite the newline character in each stri ng */ text[strlen(text)-1] = '\0'; substring[strlen(substring)-1] = '\0'; int i; for(i = 0 ; (text[i] = toupper(text[i])) ; i++); for(i = 0 ; (substring[i] = toupper(substring[i] )) ; i++); printf("\nThe second string %s found in the fi rst.",((strstr(text, substring) == NULL) ? "was n ot" : "was")); return 0; }


The functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional. This volume of IEEE Std 1003.1-2001 defers to the ISO C standard.
The fgets() function shall read bytes from stream into the array pointed to by s, until n-1 bytes are read, or a <newline> is read and transferred to s, or an end-of-file condition is encountered. The string is then terminated with a null byte.

The fgets() function may mark the st_atime field of the file associated with stream for update. The st_atime field shall be marked for update by the first successful execution of fgetc(), fgets(), fgetwc(), fgetws(), fread(), fscanf(), getc(), getchar(), gets(), or scanf() using stream that returns data not supplied by a prior call to ungetc() or ungetwc().

Upon successful completion, fgets() shall return s. If the stream is at end-of-file, the end-of-file indicator for the stream shall be set and fgets() shall return a null pointer. If a read error occurs, the error indicator for the stream shall be set, fgets() shall return a null pointer, [CX] and shall set errno to indicate the error.

Refer to fgetc().

3. 11. 1. Find occurrences of one string in another
#include <stdio.h> #include <string.h> #include <ctype.h>

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->