String Operator

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View String Operator as PDF for free.

More details

  • Words: 906
  • Pages: 4
String operator: get length, compare and find a char

char str[10]; int i;

#include <stdio.h> #include <string.h>

printf("Enter a string: "); fgets(str, 10, stdin);

int main(void) { char s1[80], s2[80];

/* remove newline, if presen t */ i = strlen(str) - 1;

gets(s1); gets(s2); printf("lengths: %d %d\n", strle n(s1), strlen(s2)); if(!strcmp(s1, s2)) printf("The strings are equal\ n");

if(str[i] =='\n') str[i] = '\0'; printf("This is your string: % s", str); return 0; }

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; }

Check string length and set string end

Use fwrite and fread to save and read #include <stdio.h> #include <stdlib.h>

#include <stdio.h> #include <string.h>

int main(void) { FILE *fp; int i;

int main(void) {

/* open file for output */ if((fp = fopen("my.txt", "wb"))==NUL L) {

printf("Cannot open file.\n"); exit(1); }

}

i = 100;

fclose(fp);

if(fwrite(&i, 2, 1, fp) != 1) { printf("Write error occurred.\n"); exit(1); } fclose(fp);

printf("%s", str);

return 0; }

/* 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; }

Get a string from a stream: how to use fgets #include <stdio.h>

Get string from file

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;

#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 */

How to use struct struct student { char name[30]; float number; } student1, student2;

. 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.

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: ");

main () { struct student student3; char s1[30]; float f; scanf ("%s", s1); scanf ("%f", &f);

fgets(line, sizeof(line), stdin); printf("The length of the line is: %d\n", str len(line)); return (0); }

NAME fgets - get a string from a stream

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

DESCRIPTION [CX]

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 is read and transferred to s, or an end-of-file condition is encountered. The string is then terminated with a null byte. [CX]

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().

RETURN VALUE 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.

ERRORS Refer to fgetc().

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

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; }

Related Documents

String Operator
November 2019 21
Operator
May 2020 27
String
June 2020 26
Operator C++
July 2020 15
Panasonic Operator
November 2019 25