Sie sind auf Seite 1von 4

String operator: get length, char str[10];

compare and find a char int i;

#include <stdio.h> printf("Enter a string: ");


#include <string.h> fgets(str, 10, stdin);
int main(void)
{ /* remove newline, if presen
char s1[80], s2[80]; t */
i = strlen(str) - 1;
gets(s1);
gets(s2); if(str[i] =='\n')
str[i] = '\0';
printf("lengths: %d %d\n", strle
n(s1), strlen(s2));
printf("This is your string: %
if(!strcmp(s1, s2)) s", str);
printf("The strings are equal\
n"); 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;
} Use fwrite and fread to save
and read
#include <stdio.h>
#include <stdlib.h>
Check string length and set
string end int main(void)
{
#include <stdio.h> FILE *fp;
#include <string.h> int i;

/* open file for output */


int main(void) if((fp = fopen("my.txt", "wb"))==NUL
{ L) {
printf("Cannot open file.\n"); printf("%s", str);
exit(1); }
}

i = 100; fclose(fp);

if(fwrite(&i, 2, 1, fp) != 1) { return 0;


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; Get a string from a stream: how to


} use fgets

#include <stdio.h>

int main()
{
FILE *file;
Get string from file char string [100];

file = fopen ("my.txt" , "r");


#include <stdio.h> if (file == NULL)
perror ("Error reading file");
#include <stdlib.h> else {
fgets (string , 100 , file);
int main(int argc, char *argv[]) puts (string);
fclose (file);
{ }
FILE *fp; return 0;
}
char str[128];

if((fp = fopen(argv[ 1 ], "r"))== Read formatted data from a stream:


how to use fscanf
NULL) {
printf("Cannot open file.\n"); #include <stdio.h>
exit(1);
} int main ()
{
char str[80];
while(!feof(fp)) { float f;
FILE *file;
if(fgets(str, 126, fp))
student1.name = s1;
file = fopen ("my.txt","w+");
fprintf (file, "%f %s", 3.14, "PI"); student2.number = f;
rewind (file);
fscanf (file, "%f", &f);
fscanf (file, "%s", str);
printf ("Name = %s \n", studen
fclose (file); t1.name);
printf (" %f and %s are added. \n",f,str); printf ("Number = %f \n", stud
return 0;
} ent2.number);
}
strcmp: lexicographically compares two
strings

//Declaration: int strcmp(const char *str1, con


st char *str2);
//Return:
utcome:
returns an integer based on the o
. 3. 2. Reading Strings
< 0 : str1 is less than str2
0 : str1 is equal to str2 The standard function fgets can be
>0 : str1 is greater than str2
used to read a string from the keyboard.
#include<stdio.h> The general form of an fgets call is:
#include<string.h>
fgets(name, sizeof(name), stdi
int main(void){
char s[80];
n);

printf("Enter password: ");


gets(s);
The arguments are:
if(strcmp(s, "pass")) {
printf("Invalid Password\n"); is the name of a
name
return 0; character array.
} indicates the maximum
return 1;
} sizeof(name) number of characters to
read.
/* stdin is the file to read.
Enter password: 34
Invalid Password
*/
Read a line from the keyboard and
reports its length.
How to use struct
#include <string.h>
struct student { #include <stdio.h>

char name[30]; int main()


float number; {
char line[100]; /* Line we are looking at *
} student1, student2; /
printf("Enter a line: ");
main () {
fgets(line, sizeof(line), stdin);

struct student student3; printf("The length of the line is: %d\n", str
len(line));
char s1[30];
float f; return (0);
scanf ("%s", s1); }

scanf ("%f", &f);


NAME
int main(void)
{
fgets - get a string from a stream
char text[100];
char substring[40];
SYNOPSIS
printf("\nEnter the string to be searched(less
#include <stdio.h> than 100 characters):\n");
fgets(text, sizeof(text), stdin);
char *fgets(char *restrict s, int n, FILE *restrict
stream); printf("\nEnter the string sought (less than 40
characters ):\n");
DESCRIPTION fgets(substring, sizeof(substring), stdin);

[CX]
/* overwrite the newline character in each stri
The functionality described on this ng */
reference page is aligned with the ISO C text[strlen(text)-1] = '\0';
standard. Any conflict between the substring[strlen(substring)-1] = '\0';
requirements described here and the
ISO C standard is unintentional. This int i;
for(i = 0 ; (text[i] = toupper(text[i])) ; i++);
volume of IEEE Std 1003.1-2001 defers to for(i = 0 ; (substring[i] = toupper(substring[i]
the ISO C standard. )) ; i++);

The fgets() function shall read bytes from printf("\nThe second string %s found in the fi
stream into the array pointed to by s, until n-1 rst.",((strstr(text, substring) == NULL) ? "was n
bytes are read, or a <newline> is read and ot" : "was"));
transferred to s, or an end-of-file condition is return 0;
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 <ctype.h>

Das könnte Ihnen auch gefallen