String Flashcards

1
Q

String

A

a character array terminated by a ‘\0’ (null (zero) character)
null character denotes the string termination

char name[]={‘H’,’A’,’I’,’\0’}; -‘\0’ is must in this way

mem location 2000,2001,2001,2003
or
char name[]=”HAI”; -double quote is a must,’\0’ is created automatically by the computer

or

using format specifier “%s”

char name[]=”shraddha”;
printf(“%s”,name);

char name[40];
scanf(“%s”,name); - here &name is not used since name itself is a pointer array

void printstring(char arr[]);
int main() {
char name[20]=”hello”;
printf(“your name is :%s\n”,name);

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

loop through string

A

include <stdio.h></stdio.h>

void printstring(char arr[]);
int main() {
char firstname[]=”Amrithesh”;
char secondname[]=”Venugopalan”;

printstring(firstname);
printstring(secondname);
} void printstring(char arr[]){
for (int i=0;arr[i]!='\0';i++){
    printf("%c",arr[i]);
}
printf("\n"); }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Problem with scanf and string

A

scanf() cannot input multi-word strings with spaces

here
gets() or fgets() & puts() come into the picture

char name[50];
gets(name);
puts(name);

int main() {
char name[20];
gets(name);
printf(“my name is :”);
puts(name);

}
but gets is outdated so we use fgets()

fgets(str,n,file); -stops when n-1 chars input or new line is entered

fgets(name,100,stdin);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

string using pointers

A

char *str=”Hello world”;

stores string in memory and the assigned address is stored in the char pointer str pointer points to the first mem location that is ‘H’

string initialized with pointers can be reinitialized but this is not possible with that of an array like

char arr[]=”Hello world”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Count of a string

A

include <stdio.h></stdio.h>

int main() {
char name[50];
int count=0;
gets(name);
for (int i=0;name[i]!=’\0’;i++){
count++;
}
printf(“count is :%d”,count);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Count of a string

A

include <stdio.h></stdio.h>

int main() {
char name[50];
int count=0;
gets(name);
for (int i=0;name[i]!=’\0’;i++){
count++;
}
printf(“count is :%d”,count);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Standard Library functions in C

A

library

<math.h>
<string.h>

<string.h>
strlen(str)-count number of character excluding '\0'

strcpy(new str,old str)-copies value of old string to the new string

strcat(str 1,str 2)-concatenate second string to the first one ,but we have to make sure that the size of str1>len(str 1)+len(str 2)

#include <stdio.h>
#include<string.h>
int main() {
char fstr[20]="Hello";
char sstr[]=" world";
strcat(fstr,sstr);
puts(fstr);
}

strcmp(str1,str2)-compare two string by ascii key and returns
0- if both string equal
\+ve-if first string has a greater ascii key
-ve-if second string has a greater ascii key

if first chara of both the string same then the second chara is comparedf
</string.h></stdio.h></string.h></string.h></math.h>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

taking user input character by character

A

include <stdio.h></stdio.h>

int main() {
char str[100];
char ch;
int i=0;

printf("enter character :");

while (ch != '\n')
{
    
    scanf("%c",&ch);
    str[i]=ch;
    i++;
}
str[i]='\0';
puts(str);
printf("the length of string is : %d",i+1);

}

here the end case is taken when the user press ENTER (‘\n’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

salting in C

A

include <stdio.h></stdio.h>

#include <string.h></string.h>

void salting(char pass[]);
int main() {
char password[100];
printf(“enter the password :\n”);
scanf(“%s”,&password);
salting(password);

}
void salting(char pass[]){
char salt[]=”123”;
char finalpass[100];

strcpy(finalpass,pass);
strcat(finalpass,salt);
printf("your final password is : ");
puts(finalpass); }

salting is a technique that companies use to prevent hacking they salt up our password before storing it in their database

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Slice function

A

include <stdio.h></stdio.h>

#include <string.h></string.h>

void slice(char word[],int start,int end);
int main() {
int m=2,n=7;
char name[20];
printf(“enter name : “);
scanf(“%s”,name);
slice(name,m,n);

}
void slice(char word[],int start,int end){
char sliced[20];
int x=0;
for (int i=start;i<=end;i++,x++){
sliced[x]=word[i];
}
sliced[x]=’\0’;
printf(“sliced from %d to %d is : %s”,start,end,sliced);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Vowels

A

include <stdio.h></stdio.h>

#include <string.h></string.h>

void countvowels(char word[]);
int main() {
char word[20];
printf(“enter the word : “);
scanf(“%s”,&word);
countvowels(word);

}
void countvowels(char word[]){
int count=0;
for (int i=0;word[i]!=’\0’;i++){
if (word[i]==’a’ || word[i]==’e’ || word[i]==’i’ || word[i]==’o’ || word[i]==’u’){
count+=1;
}
}
printf(“no of vowels is : %d”,count);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

ctype.h in

A

The ctype.h library defines a number of functions that operate on single characters, including:

isalnum() - returns true if the character is alphanumeric (either a letter or a digit)
isalpha() - returns true if the character is a letter
isdigit() - returns true if the character is a digit
islower() - returns true if the character is a lowercase letter
isspace() - returns true if the character is whitespace (e.g., space, tab, newline)
isupper() - returns true if the character is an uppercase letter
tolower() - converts an uppercase letter to lowercase
toupper() - converts a lowercase letter to uppercase

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

upper to lower and lower to upper

A

include <stdio.h></stdio.h>

#include <string.h>
#include <ctype.h></ctype.h></string.h>

void convertchara(char word[]);
int main() {
char word[20];
printf(“enter the word :”);
fgets(word,20,stdin);
convertchara(word);
}

void convertchara(char word[]){
for(int i=0;word[i]!=’\0’;i++){
if (islower(word[i])){
word[i]=toupper(word[i]);
}
else {
word[i]=tolower(word[i]);
}
}
puts(word);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

character with max frequency

A

include <stdio.h></stdio.h>

#include <string.h>
#include <ctype.h></ctype.h></string.h>

int main() {
char word[20];
int maxcount=0;
char maxfreq;
printf(“word : “);
fgets(word,20,stdin);
for(int i=0;word[i]!=’\0’;i++){
int count=0;
for(int j=0;word[j]!=’\0’;j++){
if (word[i]==word[j]){
count+=1;
}
}
if(count>maxcount){
maxcount=count;
maxfreq=word[i];
}
}
printf(“character : %c frequency : %d”,maxfreq,maxcount);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

remove spaces

A

include <stdio.h></stdio.h>

#include <string.h>
#include <ctype.h></ctype.h></string.h>

void removespace(char word[]);
int main() {
char word[20];
printf(“enter word :”);
fgets(word,20,stdin);
removespace(word);
printf(“word is : %s”,word);

}
void removespace(char word[]){
int i,j;
for (i=0,j=0;word[i]!=’\0’;i++){
if (word[i]!=’ ‘){
word[j]=word[i];
j++;
}
}
word[j]=’\0’;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly